2000-10-12 Elena Zannoni <ezannoni@kwikemart.cygnus.com>
[binutils-gdb.git] / gdb / symtab.c
1 /* Symbol table lookup for the GNU debugger, GDB.
2 Copyright 1986, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 1998
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "defs.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "gdbcore.h"
26 #include "frame.h"
27 #include "target.h"
28 #include "value.h"
29 #include "symfile.h"
30 #include "objfiles.h"
31 #include "gdbcmd.h"
32 #include "call-cmds.h"
33 #include "gdb_regex.h"
34 #include "expression.h"
35 #include "language.h"
36 #include "demangle.h"
37 #include "inferior.h"
38
39 #include "obstack.h"
40
41 #include <sys/types.h>
42 #include <fcntl.h>
43 #include "gdb_string.h"
44 #include "gdb_stat.h"
45 #include <ctype.h>
46
47 /* Prototype for one function in parser-defs.h,
48 instead of including that entire file. */
49
50 extern char *find_template_name_end (char *);
51
52 /* Prototypes for local functions */
53
54 static int find_methods (struct type *, char *, struct symbol **);
55
56 static void completion_list_add_name (char *, char *, int, char *, char *);
57
58 static void build_canonical_line_spec (struct symtab_and_line *,
59 char *, char ***);
60
61 static struct symtabs_and_lines decode_line_2 (struct symbol *[],
62 int, int, char ***);
63
64 static void rbreak_command (char *, int);
65
66 static void types_info (char *, int);
67
68 static void functions_info (char *, int);
69
70 static void variables_info (char *, int);
71
72 static void sources_info (char *, int);
73
74 static void output_source_filename (char *, int *);
75
76 char *operator_chars (char *, char **);
77
78 static int find_line_common (struct linetable *, int, int *);
79
80 static struct partial_symbol *lookup_partial_symbol (struct partial_symtab *,
81 const char *, int,
82 namespace_enum);
83
84 static struct symtab *lookup_symtab_1 (char *);
85
86 static struct symbol *lookup_symbol_aux (const char *name, const
87 struct block *block, const
88 namespace_enum namespace, int
89 *is_a_field_of_this, struct
90 symtab **symtab);
91
92
93 static void cplusplus_hint (char *);
94
95 static struct symbol *find_active_alias (struct symbol *sym, CORE_ADDR addr);
96
97 /* This flag is used in hppa-tdep.c, and set in hp-symtab-read.c */
98 /* Signals the presence of objects compiled by HP compilers */
99 int hp_som_som_object_present = 0;
100
101 static void fixup_section (struct general_symbol_info *, struct objfile *);
102
103 static int file_matches (char *, char **, int);
104
105 static void print_symbol_info (namespace_enum,
106 struct symtab *, struct symbol *, int, char *);
107
108 static void print_msymbol_info (struct minimal_symbol *);
109
110 static void symtab_symbol_info (char *, namespace_enum, int);
111
112 static void overload_list_add_symbol (struct symbol *sym, char *oload_name);
113
114 void _initialize_symtab (void);
115
116 /* */
117
118 /* The single non-language-specific builtin type */
119 struct type *builtin_type_error;
120
121 /* Block in which the most recently searched-for symbol was found.
122 Might be better to make this a parameter to lookup_symbol and
123 value_of_this. */
124
125 const struct block *block_found;
126
127 char no_symtab_msg[] = "No symbol table is loaded. Use the \"file\" command.";
128
129 /* While the C++ support is still in flux, issue a possibly helpful hint on
130 using the new command completion feature on single quoted demangled C++
131 symbols. Remove when loose ends are cleaned up. FIXME -fnf */
132
133 static void
134 cplusplus_hint (char *name)
135 {
136 while (*name == '\'')
137 name++;
138 printf_filtered ("Hint: try '%s<TAB> or '%s<ESC-?>\n", name, name);
139 printf_filtered ("(Note leading single quote.)\n");
140 }
141
142 /* Check for a symtab of a specific name; first in symtabs, then in
143 psymtabs. *If* there is no '/' in the name, a match after a '/'
144 in the symtab filename will also work. */
145
146 static struct symtab *
147 lookup_symtab_1 (char *name)
148 {
149 register struct symtab *s;
150 register struct partial_symtab *ps;
151 register char *slash;
152 register struct objfile *objfile;
153
154 got_symtab:
155
156 /* First, search for an exact match */
157
158 ALL_SYMTABS (objfile, s)
159 if (STREQ (name, s->filename))
160 return s;
161
162 slash = strchr (name, '/');
163
164 /* Now, search for a matching tail (only if name doesn't have any dirs) */
165
166 if (!slash)
167 ALL_SYMTABS (objfile, s)
168 {
169 char *p = s->filename;
170 char *tail = strrchr (p, '/');
171
172 if (tail)
173 p = tail + 1;
174
175 if (STREQ (p, name))
176 return s;
177 }
178
179 /* Same search rules as above apply here, but now we look thru the
180 psymtabs. */
181
182 ps = lookup_partial_symtab (name);
183 if (!ps)
184 return (NULL);
185
186 if (ps->readin)
187 error ("Internal: readin %s pst for `%s' found when no symtab found.",
188 ps->filename, name);
189
190 s = PSYMTAB_TO_SYMTAB (ps);
191
192 if (s)
193 return s;
194
195 /* At this point, we have located the psymtab for this file, but
196 the conversion to a symtab has failed. This usually happens
197 when we are looking up an include file. In this case,
198 PSYMTAB_TO_SYMTAB doesn't return a symtab, even though one has
199 been created. So, we need to run through the symtabs again in
200 order to find the file.
201 XXX - This is a crock, and should be fixed inside of the the
202 symbol parsing routines. */
203 goto got_symtab;
204 }
205
206 /* Lookup the symbol table of a source file named NAME. Try a couple
207 of variations if the first lookup doesn't work. */
208
209 struct symtab *
210 lookup_symtab (char *name)
211 {
212 register struct symtab *s;
213 #if 0
214 register char *copy;
215 #endif
216
217 s = lookup_symtab_1 (name);
218 if (s)
219 return s;
220
221 #if 0
222 /* This screws c-exp.y:yylex if there is both a type "tree" and a symtab
223 "tree.c". */
224
225 /* If name not found as specified, see if adding ".c" helps. */
226 /* Why is this? Is it just a user convenience? (If so, it's pretty
227 questionable in the presence of C++, FORTRAN, etc.). It's not in
228 the GDB manual. */
229
230 copy = (char *) alloca (strlen (name) + 3);
231 strcpy (copy, name);
232 strcat (copy, ".c");
233 s = lookup_symtab_1 (copy);
234 if (s)
235 return s;
236 #endif /* 0 */
237
238 /* We didn't find anything; die. */
239 return 0;
240 }
241
242 /* Lookup the partial symbol table of a source file named NAME.
243 *If* there is no '/' in the name, a match after a '/'
244 in the psymtab filename will also work. */
245
246 struct partial_symtab *
247 lookup_partial_symtab (char *name)
248 {
249 register struct partial_symtab *pst;
250 register struct objfile *objfile;
251
252 ALL_PSYMTABS (objfile, pst)
253 {
254 if (STREQ (name, pst->filename))
255 {
256 return (pst);
257 }
258 }
259
260 /* Now, search for a matching tail (only if name doesn't have any dirs) */
261
262 if (!strchr (name, '/'))
263 ALL_PSYMTABS (objfile, pst)
264 {
265 char *p = pst->filename;
266 char *tail = strrchr (p, '/');
267
268 if (tail)
269 p = tail + 1;
270
271 if (STREQ (p, name))
272 return (pst);
273 }
274
275 return (NULL);
276 }
277 \f
278 /* Mangle a GDB method stub type. This actually reassembles the pieces of the
279 full method name, which consist of the class name (from T), the unadorned
280 method name from METHOD_ID, and the signature for the specific overload,
281 specified by SIGNATURE_ID. Note that this function is g++ specific. */
282
283 char *
284 gdb_mangle_name (struct type *type, int method_id, int signature_id)
285 {
286 int mangled_name_len;
287 char *mangled_name;
288 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
289 struct fn_field *method = &f[signature_id];
290 char *field_name = TYPE_FN_FIELDLIST_NAME (type, method_id);
291 char *physname = TYPE_FN_FIELD_PHYSNAME (f, signature_id);
292 char *newname = type_name_no_tag (type);
293
294 /* Does the form of physname indicate that it is the full mangled name
295 of a constructor (not just the args)? */
296 int is_full_physname_constructor;
297
298 int is_constructor;
299 int is_destructor = DESTRUCTOR_PREFIX_P (physname);
300 /* Need a new type prefix. */
301 char *const_prefix = method->is_const ? "C" : "";
302 char *volatile_prefix = method->is_volatile ? "V" : "";
303 char buf[20];
304 int len = (newname == NULL ? 0 : strlen (newname));
305
306 is_full_physname_constructor =
307 ((physname[0] == '_' && physname[1] == '_' &&
308 (isdigit (physname[2]) || physname[2] == 'Q' || physname[2] == 't'))
309 || (strncmp (physname, "__ct", 4) == 0));
310
311 is_constructor =
312 is_full_physname_constructor || (newname && STREQ (field_name, newname));
313
314 if (!is_destructor)
315 is_destructor = (strncmp (physname, "__dt", 4) == 0);
316
317 if (is_destructor || is_full_physname_constructor)
318 {
319 mangled_name = (char *) xmalloc (strlen (physname) + 1);
320 strcpy (mangled_name, physname);
321 return mangled_name;
322 }
323
324 if (len == 0)
325 {
326 sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
327 }
328 else if (physname[0] == 't' || physname[0] == 'Q')
329 {
330 /* The physname for template and qualified methods already includes
331 the class name. */
332 sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
333 newname = NULL;
334 len = 0;
335 }
336 else
337 {
338 sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len);
339 }
340 mangled_name_len = ((is_constructor ? 0 : strlen (field_name))
341 + strlen (buf) + len
342 + strlen (physname)
343 + 1);
344
345 /* Only needed for GNU-mangled names. ANSI-mangled names
346 work with the normal mechanisms. */
347 if (OPNAME_PREFIX_P (field_name))
348 {
349 const char *opname = cplus_mangle_opname (field_name + 3, 0);
350 if (opname == NULL)
351 error ("No mangling for \"%s\"", field_name);
352 mangled_name_len += strlen (opname);
353 mangled_name = (char *) xmalloc (mangled_name_len);
354
355 strncpy (mangled_name, field_name, 3);
356 mangled_name[3] = '\0';
357 strcat (mangled_name, opname);
358 }
359 else
360 {
361 mangled_name = (char *) xmalloc (mangled_name_len);
362 if (is_constructor)
363 mangled_name[0] = '\0';
364 else
365 strcpy (mangled_name, field_name);
366 }
367 strcat (mangled_name, buf);
368 /* If the class doesn't have a name, i.e. newname NULL, then we just
369 mangle it using 0 for the length of the class. Thus it gets mangled
370 as something starting with `::' rather than `classname::'. */
371 if (newname != NULL)
372 strcat (mangled_name, newname);
373
374 strcat (mangled_name, physname);
375 return (mangled_name);
376 }
377 \f
378
379
380 /* Find which partial symtab on contains PC and SECTION. Return 0 if none. */
381
382 struct partial_symtab *
383 find_pc_sect_psymtab (CORE_ADDR pc, asection *section)
384 {
385 register struct partial_symtab *pst;
386 register struct objfile *objfile;
387
388 ALL_PSYMTABS (objfile, pst)
389 {
390 if (pc >= pst->textlow && pc < pst->texthigh)
391 {
392 struct minimal_symbol *msymbol;
393 struct partial_symtab *tpst;
394
395 /* An objfile that has its functions reordered might have
396 many partial symbol tables containing the PC, but
397 we want the partial symbol table that contains the
398 function containing the PC. */
399 if (!(objfile->flags & OBJF_REORDERED) &&
400 section == 0) /* can't validate section this way */
401 return (pst);
402
403 msymbol = lookup_minimal_symbol_by_pc_section (pc, section);
404 if (msymbol == NULL)
405 return (pst);
406
407 for (tpst = pst; tpst != NULL; tpst = tpst->next)
408 {
409 if (pc >= tpst->textlow && pc < tpst->texthigh)
410 {
411 struct partial_symbol *p;
412
413 p = find_pc_sect_psymbol (tpst, pc, section);
414 if (p != NULL
415 && SYMBOL_VALUE_ADDRESS (p)
416 == SYMBOL_VALUE_ADDRESS (msymbol))
417 return (tpst);
418 }
419 }
420 return (pst);
421 }
422 }
423 return (NULL);
424 }
425
426 /* Find which partial symtab contains PC. Return 0 if none.
427 Backward compatibility, no section */
428
429 struct partial_symtab *
430 find_pc_psymtab (CORE_ADDR pc)
431 {
432 return find_pc_sect_psymtab (pc, find_pc_mapped_section (pc));
433 }
434
435 /* Find which partial symbol within a psymtab matches PC and SECTION.
436 Return 0 if none. Check all psymtabs if PSYMTAB is 0. */
437
438 struct partial_symbol *
439 find_pc_sect_psymbol (struct partial_symtab *psymtab, CORE_ADDR pc,
440 asection *section)
441 {
442 struct partial_symbol *best = NULL, *p, **pp;
443 CORE_ADDR best_pc;
444
445 if (!psymtab)
446 psymtab = find_pc_sect_psymtab (pc, section);
447 if (!psymtab)
448 return 0;
449
450 /* Cope with programs that start at address 0 */
451 best_pc = (psymtab->textlow != 0) ? psymtab->textlow - 1 : 0;
452
453 /* Search the global symbols as well as the static symbols, so that
454 find_pc_partial_function doesn't use a minimal symbol and thus
455 cache a bad endaddr. */
456 for (pp = psymtab->objfile->global_psymbols.list + psymtab->globals_offset;
457 (pp - (psymtab->objfile->global_psymbols.list + psymtab->globals_offset)
458 < psymtab->n_global_syms);
459 pp++)
460 {
461 p = *pp;
462 if (SYMBOL_NAMESPACE (p) == VAR_NAMESPACE
463 && SYMBOL_CLASS (p) == LOC_BLOCK
464 && pc >= SYMBOL_VALUE_ADDRESS (p)
465 && (SYMBOL_VALUE_ADDRESS (p) > best_pc
466 || (psymtab->textlow == 0
467 && best_pc == 0 && SYMBOL_VALUE_ADDRESS (p) == 0)))
468 {
469 if (section) /* match on a specific section */
470 {
471 fixup_psymbol_section (p, psymtab->objfile);
472 if (SYMBOL_BFD_SECTION (p) != section)
473 continue;
474 }
475 best_pc = SYMBOL_VALUE_ADDRESS (p);
476 best = p;
477 }
478 }
479
480 for (pp = psymtab->objfile->static_psymbols.list + psymtab->statics_offset;
481 (pp - (psymtab->objfile->static_psymbols.list + psymtab->statics_offset)
482 < psymtab->n_static_syms);
483 pp++)
484 {
485 p = *pp;
486 if (SYMBOL_NAMESPACE (p) == VAR_NAMESPACE
487 && SYMBOL_CLASS (p) == LOC_BLOCK
488 && pc >= SYMBOL_VALUE_ADDRESS (p)
489 && (SYMBOL_VALUE_ADDRESS (p) > best_pc
490 || (psymtab->textlow == 0
491 && best_pc == 0 && SYMBOL_VALUE_ADDRESS (p) == 0)))
492 {
493 if (section) /* match on a specific section */
494 {
495 fixup_psymbol_section (p, psymtab->objfile);
496 if (SYMBOL_BFD_SECTION (p) != section)
497 continue;
498 }
499 best_pc = SYMBOL_VALUE_ADDRESS (p);
500 best = p;
501 }
502 }
503
504 return best;
505 }
506
507 /* Find which partial symbol within a psymtab matches PC. Return 0 if none.
508 Check all psymtabs if PSYMTAB is 0. Backwards compatibility, no section. */
509
510 struct partial_symbol *
511 find_pc_psymbol (struct partial_symtab *psymtab, CORE_ADDR pc)
512 {
513 return find_pc_sect_psymbol (psymtab, pc, find_pc_mapped_section (pc));
514 }
515 \f
516 /* Debug symbols usually don't have section information. We need to dig that
517 out of the minimal symbols and stash that in the debug symbol. */
518
519 static void
520 fixup_section (struct general_symbol_info *ginfo, struct objfile *objfile)
521 {
522 struct minimal_symbol *msym;
523 msym = lookup_minimal_symbol (ginfo->name, NULL, objfile);
524
525 if (msym)
526 {
527 ginfo->bfd_section = SYMBOL_BFD_SECTION (msym);
528 ginfo->section = SYMBOL_SECTION (msym);
529 }
530 }
531
532 struct symbol *
533 fixup_symbol_section (struct symbol *sym, struct objfile *objfile)
534 {
535 if (!sym)
536 return NULL;
537
538 if (SYMBOL_BFD_SECTION (sym))
539 return sym;
540
541 fixup_section (&sym->ginfo, objfile);
542
543 return sym;
544 }
545
546 struct partial_symbol *
547 fixup_psymbol_section (struct partial_symbol *psym, struct objfile *objfile)
548 {
549 if (!psym)
550 return NULL;
551
552 if (SYMBOL_BFD_SECTION (psym))
553 return psym;
554
555 fixup_section (&psym->ginfo, objfile);
556
557 return psym;
558 }
559
560 /* Find the definition for a specified symbol name NAME
561 in namespace NAMESPACE, visible from lexical block BLOCK.
562 Returns the struct symbol pointer, or zero if no symbol is found.
563 If SYMTAB is non-NULL, store the symbol table in which the
564 symbol was found there, or NULL if not found.
565 C++: if IS_A_FIELD_OF_THIS is nonzero on entry, check to see if
566 NAME is a field of the current implied argument `this'. If so set
567 *IS_A_FIELD_OF_THIS to 1, otherwise set it to zero.
568 BLOCK_FOUND is set to the block in which NAME is found (in the case of
569 a field of `this', value_of_this sets BLOCK_FOUND to the proper value.) */
570
571 /* This function has a bunch of loops in it and it would seem to be
572 attractive to put in some QUIT's (though I'm not really sure
573 whether it can run long enough to be really important). But there
574 are a few calls for which it would appear to be bad news to quit
575 out of here: find_proc_desc in alpha-tdep.c and mips-tdep.c, and
576 nindy_frame_chain_valid in nindy-tdep.c. (Note that there is C++
577 code below which can error(), but that probably doesn't affect
578 these calls since they are looking for a known variable and thus
579 can probably assume it will never hit the C++ code). */
580
581 struct symbol *
582 lookup_symbol (const char *name, const struct block *block,
583 const namespace_enum namespace, int *is_a_field_of_this,
584 struct symtab **symtab)
585 {
586 char *modified_name = NULL;
587 char *modified_name2 = NULL;
588 int needtofreename = 0;
589 struct symbol *returnval;
590
591 if (case_sensitivity == case_sensitive_off)
592 {
593 char *copy;
594 int len, i;
595
596 len = strlen (name);
597 copy = (char *) alloca (len + 1);
598 for (i= 0; i < len; i++)
599 copy[i] = tolower (name[i]);
600 copy[len] = 0;
601 modified_name = copy;
602 }
603 else
604 modified_name = (char *) name;
605
606 /* If we are using C++ language, demangle the name before doing a lookup, so
607 we can always binary search. */
608 if (current_language->la_language == language_cplus)
609 {
610 modified_name2 = cplus_demangle (modified_name, DMGL_ANSI | DMGL_PARAMS);
611 if (modified_name2)
612 {
613 modified_name = modified_name2;
614 needtofreename = 1;
615 }
616 }
617
618 returnval = lookup_symbol_aux (modified_name, block, namespace,
619 is_a_field_of_this, symtab);
620 if (needtofreename)
621 free (modified_name2);
622
623 return returnval;
624 }
625
626 static struct symbol *
627 lookup_symbol_aux (const char *name, const struct block *block,
628 const namespace_enum namespace, int *is_a_field_of_this,
629 struct symtab **symtab)
630 {
631 register struct symbol *sym;
632 register struct symtab *s = NULL;
633 register struct partial_symtab *ps;
634 register struct blockvector *bv;
635 register struct objfile *objfile = NULL;
636 register struct block *b;
637 register struct minimal_symbol *msymbol;
638
639
640 /* Search specified block and its superiors. */
641
642 while (block != 0)
643 {
644 sym = lookup_block_symbol (block, name, namespace);
645 if (sym)
646 {
647 block_found = block;
648 if (symtab != NULL)
649 {
650 /* Search the list of symtabs for one which contains the
651 address of the start of this block. */
652 ALL_SYMTABS (objfile, s)
653 {
654 bv = BLOCKVECTOR (s);
655 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
656 if (BLOCK_START (b) <= BLOCK_START (block)
657 && BLOCK_END (b) > BLOCK_START (block))
658 goto found;
659 }
660 found:
661 *symtab = s;
662 }
663
664 return fixup_symbol_section (sym, objfile);
665 }
666 block = BLOCK_SUPERBLOCK (block);
667 }
668
669 /* FIXME: this code is never executed--block is always NULL at this
670 point. What is it trying to do, anyway? We already should have
671 checked the STATIC_BLOCK above (it is the superblock of top-level
672 blocks). Why is VAR_NAMESPACE special-cased? */
673 /* Don't need to mess with the psymtabs; if we have a block,
674 that file is read in. If we don't, then we deal later with
675 all the psymtab stuff that needs checking. */
676 /* Note (RT): The following never-executed code looks unnecessary to me also.
677 * If we change the code to use the original (passed-in)
678 * value of 'block', we could cause it to execute, but then what
679 * would it do? The STATIC_BLOCK of the symtab containing the passed-in
680 * 'block' was already searched by the above code. And the STATIC_BLOCK's
681 * of *other* symtabs (those files not containing 'block' lexically)
682 * should not contain 'block' address-wise. So we wouldn't expect this
683 * code to find any 'sym''s that were not found above. I vote for
684 * deleting the following paragraph of code.
685 */
686 if (namespace == VAR_NAMESPACE && block != NULL)
687 {
688 struct block *b;
689 /* Find the right symtab. */
690 ALL_SYMTABS (objfile, s)
691 {
692 bv = BLOCKVECTOR (s);
693 b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
694 if (BLOCK_START (b) <= BLOCK_START (block)
695 && BLOCK_END (b) > BLOCK_START (block))
696 {
697 sym = lookup_block_symbol (b, name, VAR_NAMESPACE);
698 if (sym)
699 {
700 block_found = b;
701 if (symtab != NULL)
702 *symtab = s;
703 return fixup_symbol_section (sym, objfile);
704 }
705 }
706 }
707 }
708
709
710 /* C++: If requested to do so by the caller,
711 check to see if NAME is a field of `this'. */
712 if (is_a_field_of_this)
713 {
714 struct value *v = value_of_this (0);
715
716 *is_a_field_of_this = 0;
717 if (v && check_field (v, name))
718 {
719 *is_a_field_of_this = 1;
720 if (symtab != NULL)
721 *symtab = NULL;
722 return NULL;
723 }
724 }
725
726 /* Now search all global blocks. Do the symtab's first, then
727 check the psymtab's. If a psymtab indicates the existence
728 of the desired name as a global, then do psymtab-to-symtab
729 conversion on the fly and return the found symbol. */
730
731 ALL_SYMTABS (objfile, s)
732 {
733 bv = BLOCKVECTOR (s);
734 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
735 sym = lookup_block_symbol (block, name, namespace);
736 if (sym)
737 {
738 block_found = block;
739 if (symtab != NULL)
740 *symtab = s;
741 return fixup_symbol_section (sym, objfile);
742 }
743 }
744
745 #ifndef HPUXHPPA
746
747 /* Check for the possibility of the symbol being a function or
748 a mangled variable that is stored in one of the minimal symbol tables.
749 Eventually, all global symbols might be resolved in this way. */
750
751 if (namespace == VAR_NAMESPACE)
752 {
753 msymbol = lookup_minimal_symbol (name, NULL, NULL);
754 if (msymbol != NULL)
755 {
756 s = find_pc_sect_symtab (SYMBOL_VALUE_ADDRESS (msymbol),
757 SYMBOL_BFD_SECTION (msymbol));
758 if (s != NULL)
759 {
760 /* This is a function which has a symtab for its address. */
761 bv = BLOCKVECTOR (s);
762 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
763 sym = lookup_block_symbol (block, SYMBOL_NAME (msymbol),
764 namespace);
765 /* We kept static functions in minimal symbol table as well as
766 in static scope. We want to find them in the symbol table. */
767 if (!sym)
768 {
769 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
770 sym = lookup_block_symbol (block, SYMBOL_NAME (msymbol),
771 namespace);
772 }
773
774 /* sym == 0 if symbol was found in the minimal symbol table
775 but not in the symtab.
776 Return 0 to use the msymbol definition of "foo_".
777
778 This happens for Fortran "foo_" symbols,
779 which are "foo" in the symtab.
780
781 This can also happen if "asm" is used to make a
782 regular symbol but not a debugging symbol, e.g.
783 asm(".globl _main");
784 asm("_main:");
785 */
786
787 if (symtab != NULL)
788 *symtab = s;
789 return fixup_symbol_section (sym, objfile);
790 }
791 else if (MSYMBOL_TYPE (msymbol) != mst_text
792 && MSYMBOL_TYPE (msymbol) != mst_file_text
793 && !STREQ (name, SYMBOL_NAME (msymbol)))
794 {
795 /* This is a mangled variable, look it up by its
796 mangled name. */
797 return lookup_symbol (SYMBOL_NAME (msymbol), block,
798 namespace, is_a_field_of_this, symtab);
799 }
800 /* There are no debug symbols for this file, or we are looking
801 for an unmangled variable.
802 Try to find a matching static symbol below. */
803 }
804 }
805
806 #endif
807
808 ALL_PSYMTABS (objfile, ps)
809 {
810 if (!ps->readin && lookup_partial_symbol (ps, name, 1, namespace))
811 {
812 s = PSYMTAB_TO_SYMTAB (ps);
813 bv = BLOCKVECTOR (s);
814 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
815 sym = lookup_block_symbol (block, name, namespace);
816 if (!sym)
817 {
818 /* This shouldn't be necessary, but as a last resort
819 * try looking in the statics even though the psymtab
820 * claimed the symbol was global. It's possible that
821 * the psymtab gets it wrong in some cases.
822 */
823 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
824 sym = lookup_block_symbol (block, name, namespace);
825 if (!sym)
826 error ("Internal: global symbol `%s' found in %s psymtab but not in symtab.\n\
827 %s may be an inlined function, or may be a template function\n\
828 (if a template, try specifying an instantiation: %s<type>).",
829 name, ps->filename, name, name);
830 }
831 if (symtab != NULL)
832 *symtab = s;
833 return fixup_symbol_section (sym, objfile);
834 }
835 }
836
837 /* Now search all static file-level symbols.
838 Not strictly correct, but more useful than an error.
839 Do the symtabs first, then check the psymtabs.
840 If a psymtab indicates the existence
841 of the desired name as a file-level static, then do psymtab-to-symtab
842 conversion on the fly and return the found symbol. */
843
844 ALL_SYMTABS (objfile, s)
845 {
846 bv = BLOCKVECTOR (s);
847 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
848 sym = lookup_block_symbol (block, name, namespace);
849 if (sym)
850 {
851 block_found = block;
852 if (symtab != NULL)
853 *symtab = s;
854 return fixup_symbol_section (sym, objfile);
855 }
856 }
857
858 ALL_PSYMTABS (objfile, ps)
859 {
860 if (!ps->readin && lookup_partial_symbol (ps, name, 0, namespace))
861 {
862 s = PSYMTAB_TO_SYMTAB (ps);
863 bv = BLOCKVECTOR (s);
864 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
865 sym = lookup_block_symbol (block, name, namespace);
866 if (!sym)
867 {
868 /* This shouldn't be necessary, but as a last resort
869 * try looking in the globals even though the psymtab
870 * claimed the symbol was static. It's possible that
871 * the psymtab gets it wrong in some cases.
872 */
873 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
874 sym = lookup_block_symbol (block, name, namespace);
875 if (!sym)
876 error ("Internal: static symbol `%s' found in %s psymtab but not in symtab.\n\
877 %s may be an inlined function, or may be a template function\n\
878 (if a template, try specifying an instantiation: %s<type>).",
879 name, ps->filename, name, name);
880 }
881 if (symtab != NULL)
882 *symtab = s;
883 return fixup_symbol_section (sym, objfile);
884 }
885 }
886
887 #ifdef HPUXHPPA
888
889 /* Check for the possibility of the symbol being a function or
890 a global variable that is stored in one of the minimal symbol tables.
891 The "minimal symbol table" is built from linker-supplied info.
892
893 RT: I moved this check to last, after the complete search of
894 the global (p)symtab's and static (p)symtab's. For HP-generated
895 symbol tables, this check was causing a premature exit from
896 lookup_symbol with NULL return, and thus messing up symbol lookups
897 of things like "c::f". It seems to me a check of the minimal
898 symbol table ought to be a last resort in any case. I'm vaguely
899 worried about the comment below which talks about FORTRAN routines "foo_"
900 though... is it saying we need to do the "minsym" check before
901 the static check in this case?
902 */
903
904 if (namespace == VAR_NAMESPACE)
905 {
906 msymbol = lookup_minimal_symbol (name, NULL, NULL);
907 if (msymbol != NULL)
908 {
909 /* OK, we found a minimal symbol in spite of not
910 * finding any symbol. There are various possible
911 * explanations for this. One possibility is the symbol
912 * exists in code not compiled -g. Another possibility
913 * is that the 'psymtab' isn't doing its job.
914 * A third possibility, related to #2, is that we were confused
915 * by name-mangling. For instance, maybe the psymtab isn't
916 * doing its job because it only know about demangled
917 * names, but we were given a mangled name...
918 */
919
920 /* We first use the address in the msymbol to try to
921 * locate the appropriate symtab. Note that find_pc_symtab()
922 * has a side-effect of doing psymtab-to-symtab expansion,
923 * for the found symtab.
924 */
925 s = find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol));
926 if (s != NULL)
927 {
928 bv = BLOCKVECTOR (s);
929 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
930 sym = lookup_block_symbol (block, SYMBOL_NAME (msymbol),
931 namespace);
932 /* We kept static functions in minimal symbol table as well as
933 in static scope. We want to find them in the symbol table. */
934 if (!sym)
935 {
936 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
937 sym = lookup_block_symbol (block, SYMBOL_NAME (msymbol),
938 namespace);
939 }
940 /* If we found one, return it */
941 if (sym)
942 {
943 if (symtab != NULL)
944 *symtab = s;
945 return sym;
946 }
947
948 /* If we get here with sym == 0, the symbol was
949 found in the minimal symbol table
950 but not in the symtab.
951 Fall through and return 0 to use the msymbol
952 definition of "foo_".
953 (Note that outer code generally follows up a call
954 to this routine with a call to lookup_minimal_symbol(),
955 so a 0 return means we'll just flow into that other routine).
956
957 This happens for Fortran "foo_" symbols,
958 which are "foo" in the symtab.
959
960 This can also happen if "asm" is used to make a
961 regular symbol but not a debugging symbol, e.g.
962 asm(".globl _main");
963 asm("_main:");
964 */
965 }
966
967 /* If the lookup-by-address fails, try repeating the
968 * entire lookup process with the symbol name from
969 * the msymbol (if different from the original symbol name).
970 */
971 else if (MSYMBOL_TYPE (msymbol) != mst_text
972 && MSYMBOL_TYPE (msymbol) != mst_file_text
973 && !STREQ (name, SYMBOL_NAME (msymbol)))
974 {
975 return lookup_symbol (SYMBOL_NAME (msymbol), block,
976 namespace, is_a_field_of_this, symtab);
977 }
978 }
979 }
980
981 #endif
982
983 if (symtab != NULL)
984 *symtab = NULL;
985 return 0;
986 }
987
988 /* Look, in partial_symtab PST, for symbol NAME. Check the global
989 symbols if GLOBAL, the static symbols if not */
990
991 static struct partial_symbol *
992 lookup_partial_symbol (struct partial_symtab *pst, const char *name, int global,
993 namespace_enum namespace)
994 {
995 struct partial_symbol *temp;
996 struct partial_symbol **start, **psym;
997 struct partial_symbol **top, **bottom, **center;
998 int length = (global ? pst->n_global_syms : pst->n_static_syms);
999 int do_linear_search = 1;
1000
1001 if (length == 0)
1002 {
1003 return (NULL);
1004 }
1005 start = (global ?
1006 pst->objfile->global_psymbols.list + pst->globals_offset :
1007 pst->objfile->static_psymbols.list + pst->statics_offset);
1008
1009 if (global) /* This means we can use a binary search. */
1010 {
1011 do_linear_search = 0;
1012
1013 /* Binary search. This search is guaranteed to end with center
1014 pointing at the earliest partial symbol with the correct
1015 name. At that point *all* partial symbols with that name
1016 will be checked against the correct namespace. */
1017
1018 bottom = start;
1019 top = start + length - 1;
1020 while (top > bottom)
1021 {
1022 center = bottom + (top - bottom) / 2;
1023 if (!(center < top))
1024 abort ();
1025 if (!do_linear_search
1026 && (SYMBOL_LANGUAGE (*center) == language_java))
1027 {
1028 do_linear_search = 1;
1029 }
1030 if (STRCMP (SYMBOL_SOURCE_NAME (*center), name) >= 0)
1031 {
1032 top = center;
1033 }
1034 else
1035 {
1036 bottom = center + 1;
1037 }
1038 }
1039 if (!(top == bottom))
1040 abort ();
1041
1042 /* djb - 2000-06-03 - Use SYMBOL_MATCHES_NAME, not a strcmp, so
1043 we don't have to force a linear search on C++. Probably holds true
1044 for JAVA as well, no way to check.*/
1045 while (SYMBOL_MATCHES_NAME (*top,name))
1046 {
1047 if (SYMBOL_NAMESPACE (*top) == namespace)
1048 {
1049 return (*top);
1050 }
1051 top++;
1052 }
1053 }
1054
1055 /* Can't use a binary search or else we found during the binary search that
1056 we should also do a linear search. */
1057
1058 if (do_linear_search)
1059 {
1060 for (psym = start; psym < start + length; psym++)
1061 {
1062 if (namespace == SYMBOL_NAMESPACE (*psym))
1063 {
1064 if (SYMBOL_MATCHES_NAME (*psym, name))
1065 {
1066 return (*psym);
1067 }
1068 }
1069 }
1070 }
1071
1072 return (NULL);
1073 }
1074
1075 /* Look up a type named NAME in the struct_namespace. The type returned
1076 must not be opaque -- i.e., must have at least one field defined
1077
1078 This code was modelled on lookup_symbol -- the parts not relevant to looking
1079 up types were just left out. In particular it's assumed here that types
1080 are available in struct_namespace and only at file-static or global blocks. */
1081
1082
1083 struct type *
1084 lookup_transparent_type (const char *name)
1085 {
1086 register struct symbol *sym;
1087 register struct symtab *s = NULL;
1088 register struct partial_symtab *ps;
1089 struct blockvector *bv;
1090 register struct objfile *objfile;
1091 register struct block *block;
1092
1093 /* Now search all the global symbols. Do the symtab's first, then
1094 check the psymtab's. If a psymtab indicates the existence
1095 of the desired name as a global, then do psymtab-to-symtab
1096 conversion on the fly and return the found symbol. */
1097
1098 ALL_SYMTABS (objfile, s)
1099 {
1100 bv = BLOCKVECTOR (s);
1101 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1102 sym = lookup_block_symbol (block, name, STRUCT_NAMESPACE);
1103 if (sym && !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1104 {
1105 return SYMBOL_TYPE (sym);
1106 }
1107 }
1108
1109 ALL_PSYMTABS (objfile, ps)
1110 {
1111 if (!ps->readin && lookup_partial_symbol (ps, name, 1, STRUCT_NAMESPACE))
1112 {
1113 s = PSYMTAB_TO_SYMTAB (ps);
1114 bv = BLOCKVECTOR (s);
1115 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1116 sym = lookup_block_symbol (block, name, STRUCT_NAMESPACE);
1117 if (!sym)
1118 {
1119 /* This shouldn't be necessary, but as a last resort
1120 * try looking in the statics even though the psymtab
1121 * claimed the symbol was global. It's possible that
1122 * the psymtab gets it wrong in some cases.
1123 */
1124 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1125 sym = lookup_block_symbol (block, name, STRUCT_NAMESPACE);
1126 if (!sym)
1127 error ("Internal: global symbol `%s' found in %s psymtab but not in symtab.\n\
1128 %s may be an inlined function, or may be a template function\n\
1129 (if a template, try specifying an instantiation: %s<type>).",
1130 name, ps->filename, name, name);
1131 }
1132 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1133 return SYMBOL_TYPE (sym);
1134 }
1135 }
1136
1137 /* Now search the static file-level symbols.
1138 Not strictly correct, but more useful than an error.
1139 Do the symtab's first, then
1140 check the psymtab's. If a psymtab indicates the existence
1141 of the desired name as a file-level static, then do psymtab-to-symtab
1142 conversion on the fly and return the found symbol.
1143 */
1144
1145 ALL_SYMTABS (objfile, s)
1146 {
1147 bv = BLOCKVECTOR (s);
1148 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1149 sym = lookup_block_symbol (block, name, STRUCT_NAMESPACE);
1150 if (sym && !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1151 {
1152 return SYMBOL_TYPE (sym);
1153 }
1154 }
1155
1156 ALL_PSYMTABS (objfile, ps)
1157 {
1158 if (!ps->readin && lookup_partial_symbol (ps, name, 0, STRUCT_NAMESPACE))
1159 {
1160 s = PSYMTAB_TO_SYMTAB (ps);
1161 bv = BLOCKVECTOR (s);
1162 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1163 sym = lookup_block_symbol (block, name, STRUCT_NAMESPACE);
1164 if (!sym)
1165 {
1166 /* This shouldn't be necessary, but as a last resort
1167 * try looking in the globals even though the psymtab
1168 * claimed the symbol was static. It's possible that
1169 * the psymtab gets it wrong in some cases.
1170 */
1171 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1172 sym = lookup_block_symbol (block, name, STRUCT_NAMESPACE);
1173 if (!sym)
1174 error ("Internal: static symbol `%s' found in %s psymtab but not in symtab.\n\
1175 %s may be an inlined function, or may be a template function\n\
1176 (if a template, try specifying an instantiation: %s<type>).",
1177 name, ps->filename, name, name);
1178 }
1179 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1180 return SYMBOL_TYPE (sym);
1181 }
1182 }
1183 return (struct type *) 0;
1184 }
1185
1186
1187 /* Find the psymtab containing main(). */
1188 /* FIXME: What about languages without main() or specially linked
1189 executables that have no main() ? */
1190
1191 struct partial_symtab *
1192 find_main_psymtab (void)
1193 {
1194 register struct partial_symtab *pst;
1195 register struct objfile *objfile;
1196
1197 ALL_PSYMTABS (objfile, pst)
1198 {
1199 if (lookup_partial_symbol (pst, "main", 1, VAR_NAMESPACE))
1200 {
1201 return (pst);
1202 }
1203 }
1204 return (NULL);
1205 }
1206
1207 /* Search BLOCK for symbol NAME in NAMESPACE.
1208
1209 Note that if NAME is the demangled form of a C++ symbol, we will fail
1210 to find a match during the binary search of the non-encoded names, but
1211 for now we don't worry about the slight inefficiency of looking for
1212 a match we'll never find, since it will go pretty quick. Once the
1213 binary search terminates, we drop through and do a straight linear
1214 search on the symbols. Each symbol which is marked as being a C++
1215 symbol (language_cplus set) has both the encoded and non-encoded names
1216 tested for a match. */
1217
1218 struct symbol *
1219 lookup_block_symbol (register const struct block *block, const char *name,
1220 const namespace_enum namespace)
1221 {
1222 register int bot, top, inc;
1223 register struct symbol *sym;
1224 register struct symbol *sym_found = NULL;
1225 register int do_linear_search = 1;
1226
1227 /* If the blocks's symbols were sorted, start with a binary search. */
1228
1229 if (BLOCK_SHOULD_SORT (block))
1230 {
1231 /* Reset the linear search flag so if the binary search fails, we
1232 won't do the linear search once unless we find some reason to
1233 do so */
1234
1235 do_linear_search = 0;
1236 top = BLOCK_NSYMS (block);
1237 bot = 0;
1238
1239 /* Advance BOT to not far before the first symbol whose name is NAME. */
1240
1241 while (1)
1242 {
1243 inc = (top - bot + 1);
1244 /* No need to keep binary searching for the last few bits worth. */
1245 if (inc < 4)
1246 {
1247 break;
1248 }
1249 inc = (inc >> 1) + bot;
1250 sym = BLOCK_SYM (block, inc);
1251 if (!do_linear_search && (SYMBOL_LANGUAGE (sym) == language_java))
1252 {
1253 do_linear_search = 1;
1254 }
1255 if (SYMBOL_SOURCE_NAME (sym)[0] < name[0])
1256 {
1257 bot = inc;
1258 }
1259 else if (SYMBOL_SOURCE_NAME (sym)[0] > name[0])
1260 {
1261 top = inc;
1262 }
1263 else if (STRCMP (SYMBOL_SOURCE_NAME (sym), name) < 0)
1264 {
1265 bot = inc;
1266 }
1267 else
1268 {
1269 top = inc;
1270 }
1271 }
1272
1273 /* Now scan forward until we run out of symbols, find one whose
1274 name is greater than NAME, or find one we want. If there is
1275 more than one symbol with the right name and namespace, we
1276 return the first one; I believe it is now impossible for us
1277 to encounter two symbols with the same name and namespace
1278 here, because blocks containing argument symbols are no
1279 longer sorted. */
1280
1281 top = BLOCK_NSYMS (block);
1282 while (bot < top)
1283 {
1284 sym = BLOCK_SYM (block, bot);
1285 if (SYMBOL_MATCHES_NAME (sym, name))
1286 return sym;
1287 bot++;
1288 }
1289 }
1290
1291 /* Here if block isn't sorted, or we fail to find a match during the
1292 binary search above. If during the binary search above, we find a
1293 symbol which is a C++ symbol, then we have re-enabled the linear
1294 search flag which was reset when starting the binary search.
1295
1296 This loop is equivalent to the loop above, but hacked greatly for speed.
1297
1298 Note that parameter symbols do not always show up last in the
1299 list; this loop makes sure to take anything else other than
1300 parameter symbols first; it only uses parameter symbols as a
1301 last resort. Note that this only takes up extra computation
1302 time on a match. */
1303
1304 if (do_linear_search)
1305 {
1306 top = BLOCK_NSYMS (block);
1307 bot = 0;
1308 while (bot < top)
1309 {
1310 sym = BLOCK_SYM (block, bot);
1311 if (SYMBOL_NAMESPACE (sym) == namespace &&
1312 SYMBOL_MATCHES_NAME (sym, name))
1313 {
1314 /* If SYM has aliases, then use any alias that is active
1315 at the current PC. If no alias is active at the current
1316 PC, then use the main symbol.
1317
1318 ?!? Is checking the current pc correct? Is this routine
1319 ever called to look up a symbol from another context?
1320
1321 FIXME: No, it's not correct. If someone sets a
1322 conditional breakpoint at an address, then the
1323 breakpoint's `struct expression' should refer to the
1324 `struct symbol' appropriate for the breakpoint's
1325 address, which may not be the PC.
1326
1327 Even if it were never called from another context,
1328 it's totally bizarre for lookup_symbol's behavior to
1329 depend on the value of the inferior's current PC. We
1330 should pass in the appropriate PC as well as the
1331 block. The interface to lookup_symbol should change
1332 to require the caller to provide a PC. */
1333
1334 if (SYMBOL_ALIASES (sym))
1335 sym = find_active_alias (sym, read_pc ());
1336
1337 sym_found = sym;
1338 if (SYMBOL_CLASS (sym) != LOC_ARG &&
1339 SYMBOL_CLASS (sym) != LOC_LOCAL_ARG &&
1340 SYMBOL_CLASS (sym) != LOC_REF_ARG &&
1341 SYMBOL_CLASS (sym) != LOC_REGPARM &&
1342 SYMBOL_CLASS (sym) != LOC_REGPARM_ADDR &&
1343 SYMBOL_CLASS (sym) != LOC_BASEREG_ARG)
1344 {
1345 break;
1346 }
1347 }
1348 bot++;
1349 }
1350 }
1351 return (sym_found); /* Will be NULL if not found. */
1352 }
1353
1354 /* Given a main symbol SYM and ADDR, search through the alias
1355 list to determine if an alias is active at ADDR and return
1356 the active alias.
1357
1358 If no alias is active, then return SYM. */
1359
1360 static struct symbol *
1361 find_active_alias (struct symbol *sym, CORE_ADDR addr)
1362 {
1363 struct range_list *r;
1364 struct alias_list *aliases;
1365
1366 /* If we have aliases, check them first. */
1367 aliases = SYMBOL_ALIASES (sym);
1368
1369 while (aliases)
1370 {
1371 if (!SYMBOL_RANGES (aliases->sym))
1372 return aliases->sym;
1373 for (r = SYMBOL_RANGES (aliases->sym); r; r = r->next)
1374 {
1375 if (r->start <= addr && r->end > addr)
1376 return aliases->sym;
1377 }
1378 aliases = aliases->next;
1379 }
1380
1381 /* Nothing found, return the main symbol. */
1382 return sym;
1383 }
1384 \f
1385
1386 /* Return the symbol for the function which contains a specified
1387 lexical block, described by a struct block BL. */
1388
1389 struct symbol *
1390 block_function (struct block *bl)
1391 {
1392 while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0)
1393 bl = BLOCK_SUPERBLOCK (bl);
1394
1395 return BLOCK_FUNCTION (bl);
1396 }
1397
1398 /* Find the symtab associated with PC and SECTION. Look through the
1399 psymtabs and read in another symtab if necessary. */
1400
1401 struct symtab *
1402 find_pc_sect_symtab (CORE_ADDR pc, asection *section)
1403 {
1404 register struct block *b;
1405 struct blockvector *bv;
1406 register struct symtab *s = NULL;
1407 register struct symtab *best_s = NULL;
1408 register struct partial_symtab *ps;
1409 register struct objfile *objfile;
1410 CORE_ADDR distance = 0;
1411
1412 /* Search all symtabs for the one whose file contains our address, and which
1413 is the smallest of all the ones containing the address. This is designed
1414 to deal with a case like symtab a is at 0x1000-0x2000 and 0x3000-0x4000
1415 and symtab b is at 0x2000-0x3000. So the GLOBAL_BLOCK for a is from
1416 0x1000-0x4000, but for address 0x2345 we want to return symtab b.
1417
1418 This happens for native ecoff format, where code from included files
1419 gets its own symtab. The symtab for the included file should have
1420 been read in already via the dependency mechanism.
1421 It might be swifter to create several symtabs with the same name
1422 like xcoff does (I'm not sure).
1423
1424 It also happens for objfiles that have their functions reordered.
1425 For these, the symtab we are looking for is not necessarily read in. */
1426
1427 ALL_SYMTABS (objfile, s)
1428 {
1429 bv = BLOCKVECTOR (s);
1430 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1431
1432 if (BLOCK_START (b) <= pc
1433 && BLOCK_END (b) > pc
1434 && (distance == 0
1435 || BLOCK_END (b) - BLOCK_START (b) < distance))
1436 {
1437 /* For an objfile that has its functions reordered,
1438 find_pc_psymtab will find the proper partial symbol table
1439 and we simply return its corresponding symtab. */
1440 /* In order to better support objfiles that contain both
1441 stabs and coff debugging info, we continue on if a psymtab
1442 can't be found. */
1443 if ((objfile->flags & OBJF_REORDERED) && objfile->psymtabs)
1444 {
1445 ps = find_pc_sect_psymtab (pc, section);
1446 if (ps)
1447 return PSYMTAB_TO_SYMTAB (ps);
1448 }
1449 if (section != 0)
1450 {
1451 int i;
1452
1453 for (i = 0; i < b->nsyms; i++)
1454 {
1455 fixup_symbol_section (b->sym[i], objfile);
1456 if (section == SYMBOL_BFD_SECTION (b->sym[i]))
1457 break;
1458 }
1459 if (i >= b->nsyms)
1460 continue; /* no symbol in this symtab matches section */
1461 }
1462 distance = BLOCK_END (b) - BLOCK_START (b);
1463 best_s = s;
1464 }
1465 }
1466
1467 if (best_s != NULL)
1468 return (best_s);
1469
1470 s = NULL;
1471 ps = find_pc_sect_psymtab (pc, section);
1472 if (ps)
1473 {
1474 if (ps->readin)
1475 /* Might want to error() here (in case symtab is corrupt and
1476 will cause a core dump), but maybe we can successfully
1477 continue, so let's not. */
1478 warning ("\
1479 (Internal error: pc 0x%s in read in psymtab, but not in symtab.)\n",
1480 paddr_nz (pc));
1481 s = PSYMTAB_TO_SYMTAB (ps);
1482 }
1483 return (s);
1484 }
1485
1486 /* Find the symtab associated with PC. Look through the psymtabs and
1487 read in another symtab if necessary. Backward compatibility, no section */
1488
1489 struct symtab *
1490 find_pc_symtab (CORE_ADDR pc)
1491 {
1492 return find_pc_sect_symtab (pc, find_pc_mapped_section (pc));
1493 }
1494 \f
1495
1496 #if 0
1497
1498 /* Find the closest symbol value (of any sort -- function or variable)
1499 for a given address value. Slow but complete. (currently unused,
1500 mainly because it is too slow. We could fix it if each symtab and
1501 psymtab had contained in it the addresses ranges of each of its
1502 sections, which also would be required to make things like "info
1503 line *0x2345" cause psymtabs to be converted to symtabs). */
1504
1505 struct symbol *
1506 find_addr_symbol (CORE_ADDR addr, struct symtab **symtabp, CORE_ADDR *symaddrp)
1507 {
1508 struct symtab *symtab, *best_symtab;
1509 struct objfile *objfile;
1510 register int bot, top;
1511 register struct symbol *sym;
1512 register CORE_ADDR sym_addr;
1513 struct block *block;
1514 int blocknum;
1515
1516 /* Info on best symbol seen so far */
1517
1518 register CORE_ADDR best_sym_addr = 0;
1519 struct symbol *best_sym = 0;
1520
1521 /* FIXME -- we should pull in all the psymtabs, too! */
1522 ALL_SYMTABS (objfile, symtab)
1523 {
1524 /* Search the global and static blocks in this symtab for
1525 the closest symbol-address to the desired address. */
1526
1527 for (blocknum = GLOBAL_BLOCK; blocknum <= STATIC_BLOCK; blocknum++)
1528 {
1529 QUIT;
1530 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), blocknum);
1531 top = BLOCK_NSYMS (block);
1532 for (bot = 0; bot < top; bot++)
1533 {
1534 sym = BLOCK_SYM (block, bot);
1535 switch (SYMBOL_CLASS (sym))
1536 {
1537 case LOC_STATIC:
1538 case LOC_LABEL:
1539 sym_addr = SYMBOL_VALUE_ADDRESS (sym);
1540 break;
1541
1542 case LOC_INDIRECT:
1543 sym_addr = SYMBOL_VALUE_ADDRESS (sym);
1544 /* An indirect symbol really lives at *sym_addr,
1545 * so an indirection needs to be done.
1546 * However, I am leaving this commented out because it's
1547 * expensive, and it's possible that symbolization
1548 * could be done without an active process (in
1549 * case this read_memory will fail). RT
1550 sym_addr = read_memory_unsigned_integer
1551 (sym_addr, TARGET_PTR_BIT / TARGET_CHAR_BIT);
1552 */
1553 break;
1554
1555 case LOC_BLOCK:
1556 sym_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
1557 break;
1558
1559 default:
1560 continue;
1561 }
1562
1563 if (sym_addr <= addr)
1564 if (sym_addr > best_sym_addr)
1565 {
1566 /* Quit if we found an exact match. */
1567 best_sym = sym;
1568 best_sym_addr = sym_addr;
1569 best_symtab = symtab;
1570 if (sym_addr == addr)
1571 goto done;
1572 }
1573 }
1574 }
1575 }
1576
1577 done:
1578 if (symtabp)
1579 *symtabp = best_symtab;
1580 if (symaddrp)
1581 *symaddrp = best_sym_addr;
1582 return best_sym;
1583 }
1584 #endif /* 0 */
1585
1586 /* Find the source file and line number for a given PC value and section.
1587 Return a structure containing a symtab pointer, a line number,
1588 and a pc range for the entire source line.
1589 The value's .pc field is NOT the specified pc.
1590 NOTCURRENT nonzero means, if specified pc is on a line boundary,
1591 use the line that ends there. Otherwise, in that case, the line
1592 that begins there is used. */
1593
1594 /* The big complication here is that a line may start in one file, and end just
1595 before the start of another file. This usually occurs when you #include
1596 code in the middle of a subroutine. To properly find the end of a line's PC
1597 range, we must search all symtabs associated with this compilation unit, and
1598 find the one whose first PC is closer than that of the next line in this
1599 symtab. */
1600
1601 /* If it's worth the effort, we could be using a binary search. */
1602
1603 struct symtab_and_line
1604 find_pc_sect_line (CORE_ADDR pc, struct sec *section, int notcurrent)
1605 {
1606 struct symtab *s;
1607 register struct linetable *l;
1608 register int len;
1609 register int i;
1610 register struct linetable_entry *item;
1611 struct symtab_and_line val;
1612 struct blockvector *bv;
1613 struct minimal_symbol *msymbol;
1614 struct minimal_symbol *mfunsym;
1615
1616 /* Info on best line seen so far, and where it starts, and its file. */
1617
1618 struct linetable_entry *best = NULL;
1619 CORE_ADDR best_end = 0;
1620 struct symtab *best_symtab = 0;
1621
1622 /* Store here the first line number
1623 of a file which contains the line at the smallest pc after PC.
1624 If we don't find a line whose range contains PC,
1625 we will use a line one less than this,
1626 with a range from the start of that file to the first line's pc. */
1627 struct linetable_entry *alt = NULL;
1628 struct symtab *alt_symtab = 0;
1629
1630 /* Info on best line seen in this file. */
1631
1632 struct linetable_entry *prev;
1633
1634 /* If this pc is not from the current frame,
1635 it is the address of the end of a call instruction.
1636 Quite likely that is the start of the following statement.
1637 But what we want is the statement containing the instruction.
1638 Fudge the pc to make sure we get that. */
1639
1640 INIT_SAL (&val); /* initialize to zeroes */
1641
1642 if (notcurrent)
1643 pc -= 1;
1644
1645 /* elz: added this because this function returned the wrong
1646 information if the pc belongs to a stub (import/export)
1647 to call a shlib function. This stub would be anywhere between
1648 two functions in the target, and the line info was erroneously
1649 taken to be the one of the line before the pc.
1650 */
1651 /* RT: Further explanation:
1652
1653 * We have stubs (trampolines) inserted between procedures.
1654 *
1655 * Example: "shr1" exists in a shared library, and a "shr1" stub also
1656 * exists in the main image.
1657 *
1658 * In the minimal symbol table, we have a bunch of symbols
1659 * sorted by start address. The stubs are marked as "trampoline",
1660 * the others appear as text. E.g.:
1661 *
1662 * Minimal symbol table for main image
1663 * main: code for main (text symbol)
1664 * shr1: stub (trampoline symbol)
1665 * foo: code for foo (text symbol)
1666 * ...
1667 * Minimal symbol table for "shr1" image:
1668 * ...
1669 * shr1: code for shr1 (text symbol)
1670 * ...
1671 *
1672 * So the code below is trying to detect if we are in the stub
1673 * ("shr1" stub), and if so, find the real code ("shr1" trampoline),
1674 * and if found, do the symbolization from the real-code address
1675 * rather than the stub address.
1676 *
1677 * Assumptions being made about the minimal symbol table:
1678 * 1. lookup_minimal_symbol_by_pc() will return a trampoline only
1679 * if we're really in the trampoline. If we're beyond it (say
1680 * we're in "foo" in the above example), it'll have a closer
1681 * symbol (the "foo" text symbol for example) and will not
1682 * return the trampoline.
1683 * 2. lookup_minimal_symbol_text() will find a real text symbol
1684 * corresponding to the trampoline, and whose address will
1685 * be different than the trampoline address. I put in a sanity
1686 * check for the address being the same, to avoid an
1687 * infinite recursion.
1688 */
1689 msymbol = lookup_minimal_symbol_by_pc (pc);
1690 if (msymbol != NULL)
1691 if (MSYMBOL_TYPE (msymbol) == mst_solib_trampoline)
1692 {
1693 mfunsym = lookup_minimal_symbol_text (SYMBOL_NAME (msymbol), NULL, NULL);
1694 if (mfunsym == NULL)
1695 /* I eliminated this warning since it is coming out
1696 * in the following situation:
1697 * gdb shmain // test program with shared libraries
1698 * (gdb) break shr1 // function in shared lib
1699 * Warning: In stub for ...
1700 * In the above situation, the shared lib is not loaded yet,
1701 * so of course we can't find the real func/line info,
1702 * but the "break" still works, and the warning is annoying.
1703 * So I commented out the warning. RT */
1704 /* warning ("In stub for %s; unable to find real function/line info", SYMBOL_NAME(msymbol)) */ ;
1705 /* fall through */
1706 else if (SYMBOL_VALUE (mfunsym) == SYMBOL_VALUE (msymbol))
1707 /* Avoid infinite recursion */
1708 /* See above comment about why warning is commented out */
1709 /* warning ("In stub for %s; unable to find real function/line info", SYMBOL_NAME(msymbol)) */ ;
1710 /* fall through */
1711 else
1712 return find_pc_line (SYMBOL_VALUE (mfunsym), 0);
1713 }
1714
1715
1716 s = find_pc_sect_symtab (pc, section);
1717 if (!s)
1718 {
1719 /* if no symbol information, return previous pc */
1720 if (notcurrent)
1721 pc++;
1722 val.pc = pc;
1723 return val;
1724 }
1725
1726 bv = BLOCKVECTOR (s);
1727
1728 /* Look at all the symtabs that share this blockvector.
1729 They all have the same apriori range, that we found was right;
1730 but they have different line tables. */
1731
1732 for (; s && BLOCKVECTOR (s) == bv; s = s->next)
1733 {
1734 /* Find the best line in this symtab. */
1735 l = LINETABLE (s);
1736 if (!l)
1737 continue;
1738 len = l->nitems;
1739 if (len <= 0)
1740 {
1741 /* I think len can be zero if the symtab lacks line numbers
1742 (e.g. gcc -g1). (Either that or the LINETABLE is NULL;
1743 I'm not sure which, and maybe it depends on the symbol
1744 reader). */
1745 continue;
1746 }
1747
1748 prev = NULL;
1749 item = l->item; /* Get first line info */
1750
1751 /* Is this file's first line closer than the first lines of other files?
1752 If so, record this file, and its first line, as best alternate. */
1753 if (item->pc > pc && (!alt || item->pc < alt->pc))
1754 {
1755 alt = item;
1756 alt_symtab = s;
1757 }
1758
1759 for (i = 0; i < len; i++, item++)
1760 {
1761 /* Leave prev pointing to the linetable entry for the last line
1762 that started at or before PC. */
1763 if (item->pc > pc)
1764 break;
1765
1766 prev = item;
1767 }
1768
1769 /* At this point, prev points at the line whose start addr is <= pc, and
1770 item points at the next line. If we ran off the end of the linetable
1771 (pc >= start of the last line), then prev == item. If pc < start of
1772 the first line, prev will not be set. */
1773
1774 /* Is this file's best line closer than the best in the other files?
1775 If so, record this file, and its best line, as best so far. */
1776
1777 if (prev && (!best || prev->pc > best->pc))
1778 {
1779 best = prev;
1780 best_symtab = s;
1781 /* If another line is in the linetable, and its PC is closer
1782 than the best_end we currently have, take it as best_end. */
1783 if (i < len && (best_end == 0 || best_end > item->pc))
1784 best_end = item->pc;
1785 }
1786 }
1787
1788 if (!best_symtab)
1789 {
1790 if (!alt_symtab)
1791 { /* If we didn't find any line # info, just
1792 return zeros. */
1793 val.pc = pc;
1794 }
1795 else
1796 {
1797 val.symtab = alt_symtab;
1798 val.line = alt->line - 1;
1799
1800 /* Don't return line 0, that means that we didn't find the line. */
1801 if (val.line == 0)
1802 ++val.line;
1803
1804 val.pc = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
1805 val.end = alt->pc;
1806 }
1807 }
1808 else
1809 {
1810 val.symtab = best_symtab;
1811 val.line = best->line;
1812 val.pc = best->pc;
1813 if (best_end && (!alt || best_end < alt->pc))
1814 val.end = best_end;
1815 else if (alt)
1816 val.end = alt->pc;
1817 else
1818 val.end = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
1819 }
1820 val.section = section;
1821 return val;
1822 }
1823
1824 /* Backward compatibility (no section) */
1825
1826 struct symtab_and_line
1827 find_pc_line (CORE_ADDR pc, int notcurrent)
1828 {
1829 asection *section;
1830
1831 section = find_pc_overlay (pc);
1832 if (pc_in_unmapped_range (pc, section))
1833 pc = overlay_mapped_address (pc, section);
1834 return find_pc_sect_line (pc, section, notcurrent);
1835 }
1836 \f
1837
1838 static struct symtab *find_line_symtab (struct symtab *, int, int *, int *);
1839
1840 /* Find line number LINE in any symtab whose name is the same as
1841 SYMTAB.
1842
1843 If found, return the symtab that contains the linetable in which it was
1844 found, set *INDEX to the index in the linetable of the best entry
1845 found, and set *EXACT_MATCH nonzero if the value returned is an
1846 exact match.
1847
1848 If not found, return NULL. */
1849
1850 static struct symtab *
1851 find_line_symtab (struct symtab *symtab, int line, int *index, int *exact_match)
1852 {
1853 int exact;
1854
1855 /* BEST_INDEX and BEST_LINETABLE identify the smallest linenumber > LINE
1856 so far seen. */
1857
1858 int best_index;
1859 struct linetable *best_linetable;
1860 struct symtab *best_symtab;
1861
1862 /* First try looking it up in the given symtab. */
1863 best_linetable = LINETABLE (symtab);
1864 best_symtab = symtab;
1865 best_index = find_line_common (best_linetable, line, &exact);
1866 if (best_index < 0 || !exact)
1867 {
1868 /* Didn't find an exact match. So we better keep looking for
1869 another symtab with the same name. In the case of xcoff,
1870 multiple csects for one source file (produced by IBM's FORTRAN
1871 compiler) produce multiple symtabs (this is unavoidable
1872 assuming csects can be at arbitrary places in memory and that
1873 the GLOBAL_BLOCK of a symtab has a begin and end address). */
1874
1875 /* BEST is the smallest linenumber > LINE so far seen,
1876 or 0 if none has been seen so far.
1877 BEST_INDEX and BEST_LINETABLE identify the item for it. */
1878 int best;
1879
1880 struct objfile *objfile;
1881 struct symtab *s;
1882
1883 if (best_index >= 0)
1884 best = best_linetable->item[best_index].line;
1885 else
1886 best = 0;
1887
1888 ALL_SYMTABS (objfile, s)
1889 {
1890 struct linetable *l;
1891 int ind;
1892
1893 if (!STREQ (symtab->filename, s->filename))
1894 continue;
1895 l = LINETABLE (s);
1896 ind = find_line_common (l, line, &exact);
1897 if (ind >= 0)
1898 {
1899 if (exact)
1900 {
1901 best_index = ind;
1902 best_linetable = l;
1903 best_symtab = s;
1904 goto done;
1905 }
1906 if (best == 0 || l->item[ind].line < best)
1907 {
1908 best = l->item[ind].line;
1909 best_index = ind;
1910 best_linetable = l;
1911 best_symtab = s;
1912 }
1913 }
1914 }
1915 }
1916 done:
1917 if (best_index < 0)
1918 return NULL;
1919
1920 if (index)
1921 *index = best_index;
1922 if (exact_match)
1923 *exact_match = exact;
1924
1925 return best_symtab;
1926 }
1927 \f
1928 /* Set the PC value for a given source file and line number and return true.
1929 Returns zero for invalid line number (and sets the PC to 0).
1930 The source file is specified with a struct symtab. */
1931
1932 int
1933 find_line_pc (struct symtab *symtab, int line, CORE_ADDR *pc)
1934 {
1935 struct linetable *l;
1936 int ind;
1937
1938 *pc = 0;
1939 if (symtab == 0)
1940 return 0;
1941
1942 symtab = find_line_symtab (symtab, line, &ind, NULL);
1943 if (symtab != NULL)
1944 {
1945 l = LINETABLE (symtab);
1946 *pc = l->item[ind].pc;
1947 return 1;
1948 }
1949 else
1950 return 0;
1951 }
1952
1953 /* Find the range of pc values in a line.
1954 Store the starting pc of the line into *STARTPTR
1955 and the ending pc (start of next line) into *ENDPTR.
1956 Returns 1 to indicate success.
1957 Returns 0 if could not find the specified line. */
1958
1959 int
1960 find_line_pc_range (struct symtab_and_line sal, CORE_ADDR *startptr,
1961 CORE_ADDR *endptr)
1962 {
1963 CORE_ADDR startaddr;
1964 struct symtab_and_line found_sal;
1965
1966 startaddr = sal.pc;
1967 if (startaddr == 0 && !find_line_pc (sal.symtab, sal.line, &startaddr))
1968 return 0;
1969
1970 /* This whole function is based on address. For example, if line 10 has
1971 two parts, one from 0x100 to 0x200 and one from 0x300 to 0x400, then
1972 "info line *0x123" should say the line goes from 0x100 to 0x200
1973 and "info line *0x355" should say the line goes from 0x300 to 0x400.
1974 This also insures that we never give a range like "starts at 0x134
1975 and ends at 0x12c". */
1976
1977 found_sal = find_pc_sect_line (startaddr, sal.section, 0);
1978 if (found_sal.line != sal.line)
1979 {
1980 /* The specified line (sal) has zero bytes. */
1981 *startptr = found_sal.pc;
1982 *endptr = found_sal.pc;
1983 }
1984 else
1985 {
1986 *startptr = found_sal.pc;
1987 *endptr = found_sal.end;
1988 }
1989 return 1;
1990 }
1991
1992 /* Given a line table and a line number, return the index into the line
1993 table for the pc of the nearest line whose number is >= the specified one.
1994 Return -1 if none is found. The value is >= 0 if it is an index.
1995
1996 Set *EXACT_MATCH nonzero if the value returned is an exact match. */
1997
1998 static int
1999 find_line_common (register struct linetable *l, register int lineno,
2000 int *exact_match)
2001 {
2002 register int i;
2003 register int len;
2004
2005 /* BEST is the smallest linenumber > LINENO so far seen,
2006 or 0 if none has been seen so far.
2007 BEST_INDEX identifies the item for it. */
2008
2009 int best_index = -1;
2010 int best = 0;
2011
2012 if (lineno <= 0)
2013 return -1;
2014 if (l == 0)
2015 return -1;
2016
2017 len = l->nitems;
2018 for (i = 0; i < len; i++)
2019 {
2020 register struct linetable_entry *item = &(l->item[i]);
2021
2022 if (item->line == lineno)
2023 {
2024 /* Return the first (lowest address) entry which matches. */
2025 *exact_match = 1;
2026 return i;
2027 }
2028
2029 if (item->line > lineno && (best == 0 || item->line < best))
2030 {
2031 best = item->line;
2032 best_index = i;
2033 }
2034 }
2035
2036 /* If we got here, we didn't get an exact match. */
2037
2038 *exact_match = 0;
2039 return best_index;
2040 }
2041
2042 int
2043 find_pc_line_pc_range (CORE_ADDR pc, CORE_ADDR *startptr, CORE_ADDR *endptr)
2044 {
2045 struct symtab_and_line sal;
2046 sal = find_pc_line (pc, 0);
2047 *startptr = sal.pc;
2048 *endptr = sal.end;
2049 return sal.symtab != 0;
2050 }
2051
2052 /* Given a function symbol SYM, find the symtab and line for the start
2053 of the function.
2054 If the argument FUNFIRSTLINE is nonzero, we want the first line
2055 of real code inside the function. */
2056
2057 static struct symtab_and_line
2058 find_function_start_sal (struct symbol *sym, int);
2059
2060 static struct symtab_and_line
2061 find_function_start_sal (struct symbol *sym, int funfirstline)
2062 {
2063 CORE_ADDR pc;
2064 struct symtab_and_line sal;
2065
2066 pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
2067 fixup_symbol_section (sym, NULL);
2068 if (funfirstline)
2069 { /* skip "first line" of function (which is actually its prologue) */
2070 asection *section = SYMBOL_BFD_SECTION (sym);
2071 /* If function is in an unmapped overlay, use its unmapped LMA
2072 address, so that SKIP_PROLOGUE has something unique to work on */
2073 if (section_is_overlay (section) &&
2074 !section_is_mapped (section))
2075 pc = overlay_unmapped_address (pc, section);
2076
2077 pc += FUNCTION_START_OFFSET;
2078 pc = SKIP_PROLOGUE (pc);
2079
2080 /* For overlays, map pc back into its mapped VMA range */
2081 pc = overlay_mapped_address (pc, section);
2082 }
2083 sal = find_pc_sect_line (pc, SYMBOL_BFD_SECTION (sym), 0);
2084
2085 #ifdef PROLOGUE_FIRSTLINE_OVERLAP
2086 /* Convex: no need to suppress code on first line, if any */
2087 sal.pc = pc;
2088 #else
2089 /* Check if SKIP_PROLOGUE left us in mid-line, and the next
2090 line is still part of the same function. */
2091 if (sal.pc != pc
2092 && BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) <= sal.end
2093 && sal.end < BLOCK_END (SYMBOL_BLOCK_VALUE (sym)))
2094 {
2095 /* First pc of next line */
2096 pc = sal.end;
2097 /* Recalculate the line number (might not be N+1). */
2098 sal = find_pc_sect_line (pc, SYMBOL_BFD_SECTION (sym), 0);
2099 }
2100 sal.pc = pc;
2101 #endif
2102
2103 return sal;
2104 }
2105 \f
2106 /* If P is of the form "operator[ \t]+..." where `...' is
2107 some legitimate operator text, return a pointer to the
2108 beginning of the substring of the operator text.
2109 Otherwise, return "". */
2110 char *
2111 operator_chars (char *p, char **end)
2112 {
2113 *end = "";
2114 if (strncmp (p, "operator", 8))
2115 return *end;
2116 p += 8;
2117
2118 /* Don't get faked out by `operator' being part of a longer
2119 identifier. */
2120 if (isalpha (*p) || *p == '_' || *p == '$' || *p == '\0')
2121 return *end;
2122
2123 /* Allow some whitespace between `operator' and the operator symbol. */
2124 while (*p == ' ' || *p == '\t')
2125 p++;
2126
2127 /* Recognize 'operator TYPENAME'. */
2128
2129 if (isalpha (*p) || *p == '_' || *p == '$')
2130 {
2131 register char *q = p + 1;
2132 while (isalnum (*q) || *q == '_' || *q == '$')
2133 q++;
2134 *end = q;
2135 return p;
2136 }
2137
2138 switch (*p)
2139 {
2140 case '!':
2141 case '=':
2142 case '*':
2143 case '/':
2144 case '%':
2145 case '^':
2146 if (p[1] == '=')
2147 *end = p + 2;
2148 else
2149 *end = p + 1;
2150 return p;
2151 case '<':
2152 case '>':
2153 case '+':
2154 case '-':
2155 case '&':
2156 case '|':
2157 if (p[1] == '=' || p[1] == p[0])
2158 *end = p + 2;
2159 else
2160 *end = p + 1;
2161 return p;
2162 case '~':
2163 case ',':
2164 *end = p + 1;
2165 return p;
2166 case '(':
2167 if (p[1] != ')')
2168 error ("`operator ()' must be specified without whitespace in `()'");
2169 *end = p + 2;
2170 return p;
2171 case '?':
2172 if (p[1] != ':')
2173 error ("`operator ?:' must be specified without whitespace in `?:'");
2174 *end = p + 2;
2175 return p;
2176 case '[':
2177 if (p[1] != ']')
2178 error ("`operator []' must be specified without whitespace in `[]'");
2179 *end = p + 2;
2180 return p;
2181 default:
2182 error ("`operator %s' not supported", p);
2183 break;
2184 }
2185 *end = "";
2186 return *end;
2187 }
2188
2189 /* Return the number of methods described for TYPE, including the
2190 methods from types it derives from. This can't be done in the symbol
2191 reader because the type of the baseclass might still be stubbed
2192 when the definition of the derived class is parsed. */
2193
2194 static int total_number_of_methods (struct type *type);
2195
2196 static int
2197 total_number_of_methods (struct type *type)
2198 {
2199 int n;
2200 int count;
2201
2202 CHECK_TYPEDEF (type);
2203 if (TYPE_CPLUS_SPECIFIC (type) == NULL)
2204 return 0;
2205 count = TYPE_NFN_FIELDS_TOTAL (type);
2206
2207 for (n = 0; n < TYPE_N_BASECLASSES (type); n++)
2208 count += total_number_of_methods (TYPE_BASECLASS (type, n));
2209
2210 return count;
2211 }
2212
2213 /* Recursive helper function for decode_line_1.
2214 Look for methods named NAME in type T.
2215 Return number of matches.
2216 Put matches in SYM_ARR, which should have been allocated with
2217 a size of total_number_of_methods (T) * sizeof (struct symbol *).
2218 Note that this function is g++ specific. */
2219
2220 static int
2221 find_methods (struct type *t, char *name, struct symbol **sym_arr)
2222 {
2223 int i1 = 0;
2224 int ibase;
2225 struct symbol *sym_class;
2226 char *class_name = type_name_no_tag (t);
2227
2228 /* Ignore this class if it doesn't have a name. This is ugly, but
2229 unless we figure out how to get the physname without the name of
2230 the class, then the loop can't do any good. */
2231 if (class_name
2232 && (sym_class = lookup_symbol (class_name,
2233 (struct block *) NULL,
2234 STRUCT_NAMESPACE,
2235 (int *) NULL,
2236 (struct symtab **) NULL)))
2237 {
2238 int method_counter;
2239
2240 /* FIXME: Shouldn't this just be CHECK_TYPEDEF (t)? */
2241 t = SYMBOL_TYPE (sym_class);
2242
2243 /* Loop over each method name. At this level, all overloads of a name
2244 are counted as a single name. There is an inner loop which loops over
2245 each overload. */
2246
2247 for (method_counter = TYPE_NFN_FIELDS (t) - 1;
2248 method_counter >= 0;
2249 --method_counter)
2250 {
2251 int field_counter;
2252 char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
2253 char dem_opname[64];
2254
2255 if (strncmp (method_name, "__", 2) == 0 ||
2256 strncmp (method_name, "op", 2) == 0 ||
2257 strncmp (method_name, "type", 4) == 0)
2258 {
2259 if (cplus_demangle_opname (method_name, dem_opname, DMGL_ANSI))
2260 method_name = dem_opname;
2261 else if (cplus_demangle_opname (method_name, dem_opname, 0))
2262 method_name = dem_opname;
2263 }
2264
2265 if (STREQ (name, method_name))
2266 /* Find all the overloaded methods with that name. */
2267 for (field_counter = TYPE_FN_FIELDLIST_LENGTH (t, method_counter) - 1;
2268 field_counter >= 0;
2269 --field_counter)
2270 {
2271 struct fn_field *f;
2272 char *phys_name;
2273
2274 f = TYPE_FN_FIELDLIST1 (t, method_counter);
2275
2276 if (TYPE_FN_FIELD_STUB (f, field_counter))
2277 {
2278 char *tmp_name;
2279
2280 tmp_name = gdb_mangle_name (t,
2281 method_counter,
2282 field_counter);
2283 phys_name = alloca (strlen (tmp_name) + 1);
2284 strcpy (phys_name, tmp_name);
2285 free (tmp_name);
2286 }
2287 else
2288 phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
2289
2290 /* Destructor is handled by caller, dont add it to the list */
2291 if (DESTRUCTOR_PREFIX_P (phys_name))
2292 continue;
2293
2294 sym_arr[i1] = lookup_symbol (phys_name,
2295 NULL, VAR_NAMESPACE,
2296 (int *) NULL,
2297 (struct symtab **) NULL);
2298 if (sym_arr[i1])
2299 i1++;
2300 else
2301 {
2302 /* This error message gets printed, but the method
2303 still seems to be found
2304 fputs_filtered("(Cannot find method ", gdb_stdout);
2305 fprintf_symbol_filtered (gdb_stdout, phys_name,
2306 language_cplus,
2307 DMGL_PARAMS | DMGL_ANSI);
2308 fputs_filtered(" - possibly inlined.)\n", gdb_stdout);
2309 */
2310 }
2311 }
2312 }
2313 }
2314
2315 /* Only search baseclasses if there is no match yet, since names in
2316 derived classes override those in baseclasses.
2317
2318 FIXME: The above is not true; it is only true of member functions
2319 if they have the same number of arguments (??? - section 13.1 of the
2320 ARM says the function members are not in the same scope but doesn't
2321 really spell out the rules in a way I understand. In any case, if
2322 the number of arguments differ this is a case in which we can overload
2323 rather than hiding without any problem, and gcc 2.4.5 does overload
2324 rather than hiding in this case). */
2325
2326 if (i1 == 0)
2327 for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
2328 i1 += find_methods (TYPE_BASECLASS (t, ibase), name, sym_arr + i1);
2329
2330 return i1;
2331 }
2332
2333 /* Helper function for decode_line_1.
2334 Build a canonical line spec in CANONICAL if it is non-NULL and if
2335 the SAL has a symtab.
2336 If SYMNAME is non-NULL the canonical line spec is `filename:symname'.
2337 If SYMNAME is NULL the line number from SAL is used and the canonical
2338 line spec is `filename:linenum'. */
2339
2340 static void
2341 build_canonical_line_spec (struct symtab_and_line *sal, char *symname,
2342 char ***canonical)
2343 {
2344 char **canonical_arr;
2345 char *canonical_name;
2346 char *filename;
2347 struct symtab *s = sal->symtab;
2348
2349 if (s == (struct symtab *) NULL
2350 || s->filename == (char *) NULL
2351 || canonical == (char ***) NULL)
2352 return;
2353
2354 canonical_arr = (char **) xmalloc (sizeof (char *));
2355 *canonical = canonical_arr;
2356
2357 filename = s->filename;
2358 if (symname != NULL)
2359 {
2360 canonical_name = xmalloc (strlen (filename) + strlen (symname) + 2);
2361 sprintf (canonical_name, "%s:%s", filename, symname);
2362 }
2363 else
2364 {
2365 canonical_name = xmalloc (strlen (filename) + 30);
2366 sprintf (canonical_name, "%s:%d", filename, sal->line);
2367 }
2368 canonical_arr[0] = canonical_name;
2369 }
2370
2371
2372
2373 /* Find an instance of the character C in the string S that is outside
2374 of all parenthesis pairs, single-quoted strings, and double-quoted
2375 strings. */
2376 static char *
2377 find_toplevel_char (char *s, char c)
2378 {
2379 int quoted = 0; /* zero if we're not in quotes;
2380 '"' if we're in a double-quoted string;
2381 '\'' if we're in a single-quoted string. */
2382 int depth = 0; /* number of unclosed parens we've seen */
2383 char *scan;
2384
2385 for (scan = s; *scan; scan++)
2386 {
2387 if (quoted)
2388 {
2389 if (*scan == quoted)
2390 quoted = 0;
2391 else if (*scan == '\\' && *(scan + 1))
2392 scan++;
2393 }
2394 else if (*scan == c && ! quoted && depth == 0)
2395 return scan;
2396 else if (*scan == '"' || *scan == '\'')
2397 quoted = *scan;
2398 else if (*scan == '(')
2399 depth++;
2400 else if (*scan == ')' && depth > 0)
2401 depth--;
2402 }
2403
2404 return 0;
2405 }
2406
2407
2408 /* Parse a string that specifies a line number.
2409 Pass the address of a char * variable; that variable will be
2410 advanced over the characters actually parsed.
2411
2412 The string can be:
2413
2414 LINENUM -- that line number in current file. PC returned is 0.
2415 FILE:LINENUM -- that line in that file. PC returned is 0.
2416 FUNCTION -- line number of openbrace of that function.
2417 PC returned is the start of the function.
2418 VARIABLE -- line number of definition of that variable.
2419 PC returned is 0.
2420 FILE:FUNCTION -- likewise, but prefer functions in that file.
2421 *EXPR -- line in which address EXPR appears.
2422
2423 This may all be followed by an "if EXPR", which we ignore.
2424
2425 FUNCTION may be an undebuggable function found in minimal symbol table.
2426
2427 If the argument FUNFIRSTLINE is nonzero, we want the first line
2428 of real code inside a function when a function is specified, and it is
2429 not OK to specify a variable or type to get its line number.
2430
2431 DEFAULT_SYMTAB specifies the file to use if none is specified.
2432 It defaults to current_source_symtab.
2433 DEFAULT_LINE specifies the line number to use for relative
2434 line numbers (that start with signs). Defaults to current_source_line.
2435 If CANONICAL is non-NULL, store an array of strings containing the canonical
2436 line specs there if necessary. Currently overloaded member functions and
2437 line numbers or static functions without a filename yield a canonical
2438 line spec. The array and the line spec strings are allocated on the heap,
2439 it is the callers responsibility to free them.
2440
2441 Note that it is possible to return zero for the symtab
2442 if no file is validly specified. Callers must check that.
2443 Also, the line number returned may be invalid. */
2444
2445 /* We allow single quotes in various places. This is a hideous
2446 kludge, which exists because the completer can't yet deal with the
2447 lack of single quotes. FIXME: write a linespec_completer which we
2448 can use as appropriate instead of make_symbol_completion_list. */
2449
2450 struct symtabs_and_lines
2451 decode_line_1 (char **argptr, int funfirstline, struct symtab *default_symtab,
2452 int default_line, char ***canonical)
2453 {
2454 struct symtabs_and_lines values;
2455 #ifdef HPPA_COMPILER_BUG
2456 /* FIXME: The native HP 9000/700 compiler has a bug which appears
2457 when optimizing this file with target i960-vxworks. I haven't
2458 been able to construct a simple test case. The problem is that
2459 in the second call to SKIP_PROLOGUE below, the compiler somehow
2460 does not realize that the statement val = find_pc_line (...) will
2461 change the values of the fields of val. It extracts the elements
2462 into registers at the top of the block, and does not update the
2463 registers after the call to find_pc_line. You can check this by
2464 inserting a printf at the end of find_pc_line to show what values
2465 it is returning for val.pc and val.end and another printf after
2466 the call to see what values the function actually got (remember,
2467 this is compiling with cc -O, with this patch removed). You can
2468 also examine the assembly listing: search for the second call to
2469 skip_prologue; the LDO statement before the next call to
2470 find_pc_line loads the address of the structure which
2471 find_pc_line will return; if there is a LDW just before the LDO,
2472 which fetches an element of the structure, then the compiler
2473 still has the bug.
2474
2475 Setting val to volatile avoids the problem. We must undef
2476 volatile, because the HPPA native compiler does not define
2477 __STDC__, although it does understand volatile, and so volatile
2478 will have been defined away in defs.h. */
2479 #undef volatile
2480 volatile struct symtab_and_line val;
2481 #define volatile /*nothing */
2482 #else
2483 struct symtab_and_line val;
2484 #endif
2485 register char *p, *p1;
2486 char *q, *pp, *ii, *p2;
2487 #if 0
2488 char *q1;
2489 #endif
2490 register struct symtab *s;
2491
2492 register struct symbol *sym;
2493 /* The symtab that SYM was found in. */
2494 struct symtab *sym_symtab;
2495
2496 register CORE_ADDR pc;
2497 register struct minimal_symbol *msymbol;
2498 char *copy;
2499 struct symbol *sym_class;
2500 int i1;
2501 int is_quoted;
2502 int is_quote_enclosed;
2503 int has_parens;
2504 int has_if = 0;
2505 int has_comma = 0;
2506 struct symbol **sym_arr;
2507 struct type *t;
2508 char *saved_arg = *argptr;
2509 extern char *gdb_completer_quote_characters;
2510
2511 INIT_SAL (&val); /* initialize to zeroes */
2512
2513 /* Defaults have defaults. */
2514
2515 if (default_symtab == 0)
2516 {
2517 default_symtab = current_source_symtab;
2518 default_line = current_source_line;
2519 }
2520
2521 /* See if arg is *PC */
2522
2523 if (**argptr == '*')
2524 {
2525 (*argptr)++;
2526 pc = parse_and_eval_address_1 (argptr);
2527
2528 values.sals = (struct symtab_and_line *)
2529 xmalloc (sizeof (struct symtab_and_line));
2530
2531 values.nelts = 1;
2532 values.sals[0] = find_pc_line (pc, 0);
2533 values.sals[0].pc = pc;
2534 values.sals[0].section = find_pc_overlay (pc);
2535
2536 return values;
2537 }
2538
2539 /* 'has_if' is for the syntax:
2540 * (gdb) break foo if (a==b)
2541 */
2542 if ((ii = strstr (*argptr, " if ")) != NULL ||
2543 (ii = strstr (*argptr, "\tif ")) != NULL ||
2544 (ii = strstr (*argptr, " if\t")) != NULL ||
2545 (ii = strstr (*argptr, "\tif\t")) != NULL ||
2546 (ii = strstr (*argptr, " if(")) != NULL ||
2547 (ii = strstr (*argptr, "\tif( ")) != NULL)
2548 has_if = 1;
2549 /* Temporarily zap out "if (condition)" to not
2550 * confuse the parenthesis-checking code below.
2551 * This is undone below. Do not change ii!!
2552 */
2553 if (has_if)
2554 {
2555 *ii = '\0';
2556 }
2557
2558 /* Set various flags.
2559 * 'has_parens' is important for overload checking, where
2560 * we allow things like:
2561 * (gdb) break c::f(int)
2562 */
2563
2564 /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
2565
2566 is_quoted = (**argptr
2567 && strchr (gdb_completer_quote_characters, **argptr) != NULL);
2568
2569 has_parens = ((pp = strchr (*argptr, '(')) != NULL
2570 && (pp = strrchr (pp, ')')) != NULL);
2571
2572 /* Now that we're safely past the has_parens check,
2573 * put back " if (condition)" so outer layers can see it
2574 */
2575 if (has_if)
2576 *ii = ' ';
2577
2578 /* Maybe we were called with a line range FILENAME:LINENUM,FILENAME:LINENUM
2579 and we must isolate the first half. Outer layers will call again later
2580 for the second half.
2581
2582 Don't count commas that appear in argument lists of overloaded
2583 functions, or in quoted strings. It's stupid to go to this much
2584 trouble when the rest of the function is such an obvious roach hotel. */
2585 ii = find_toplevel_char (*argptr, ',');
2586 has_comma = (ii != 0);
2587
2588 /* Temporarily zap out second half to not
2589 * confuse the code below.
2590 * This is undone below. Do not change ii!!
2591 */
2592 if (has_comma)
2593 {
2594 *ii = '\0';
2595 }
2596
2597 /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
2598 /* May also be CLASS::MEMBER, or NAMESPACE::NAME */
2599 /* Look for ':', but ignore inside of <> */
2600
2601 s = NULL;
2602 p = *argptr;
2603 if (p[0] == '"')
2604 {
2605 is_quote_enclosed = 1;
2606 p++;
2607 }
2608 else
2609 is_quote_enclosed = 0;
2610 for (; *p; p++)
2611 {
2612 if (p[0] == '<')
2613 {
2614 char *temp_end = find_template_name_end (p);
2615 if (!temp_end)
2616 error ("malformed template specification in command");
2617 p = temp_end;
2618 }
2619 /* Check for the end of the first half of the linespec. End of line,
2620 a tab, a double colon or the last single colon, or a space. But
2621 if enclosed in double quotes we do not break on enclosed spaces */
2622 if (!*p
2623 || p[0] == '\t'
2624 || ((p[0] == ':')
2625 && ((p[1] == ':') || (strchr (p + 1, ':') == NULL)))
2626 || ((p[0] == ' ') && !is_quote_enclosed))
2627 break;
2628 if (p[0] == '.' && strchr (p, ':') == NULL) /* Java qualified method. */
2629 {
2630 /* Find the *last* '.', since the others are package qualifiers. */
2631 for (p1 = p; *p1; p1++)
2632 {
2633 if (*p1 == '.')
2634 p = p1;
2635 }
2636 break;
2637 }
2638 }
2639 while (p[0] == ' ' || p[0] == '\t')
2640 p++;
2641
2642 /* if the closing double quote was left at the end, remove it */
2643 if (is_quote_enclosed)
2644 {
2645 char *closing_quote = strchr (p, '"');
2646 if (closing_quote && closing_quote[1] == '\0')
2647 *closing_quote = '\0';
2648 }
2649
2650 /* Now that we've safely parsed the first half,
2651 * put back ',' so outer layers can see it
2652 */
2653 if (has_comma)
2654 *ii = ',';
2655
2656 if ((p[0] == ':' || p[0] == '.') && !has_parens)
2657 {
2658 /* C++ */
2659 /* ... or Java */
2660 if (is_quoted)
2661 *argptr = *argptr + 1;
2662 if (p[0] == '.' || p[1] == ':')
2663 {
2664 char *saved_arg2 = *argptr;
2665 char *temp_end;
2666 /* First check for "global" namespace specification,
2667 of the form "::foo". If found, skip over the colons
2668 and jump to normal symbol processing */
2669 if (p[0] == ':'
2670 && ((*argptr == p) || (p[-1] == ' ') || (p[-1] == '\t')))
2671 saved_arg2 += 2;
2672
2673 /* We have what looks like a class or namespace
2674 scope specification (A::B), possibly with many
2675 levels of namespaces or classes (A::B::C::D).
2676
2677 Some versions of the HP ANSI C++ compiler (as also possibly
2678 other compilers) generate class/function/member names with
2679 embedded double-colons if they are inside namespaces. To
2680 handle this, we loop a few times, considering larger and
2681 larger prefixes of the string as though they were single
2682 symbols. So, if the initially supplied string is
2683 A::B::C::D::foo, we have to look up "A", then "A::B",
2684 then "A::B::C", then "A::B::C::D", and finally
2685 "A::B::C::D::foo" as single, monolithic symbols, because
2686 A, B, C or D may be namespaces.
2687
2688 Note that namespaces can nest only inside other
2689 namespaces, and not inside classes. So we need only
2690 consider *prefixes* of the string; there is no need to look up
2691 "B::C" separately as a symbol in the previous example. */
2692
2693 p2 = p; /* save for restart */
2694 while (1)
2695 {
2696 /* Extract the class name. */
2697 p1 = p;
2698 while (p != *argptr && p[-1] == ' ')
2699 --p;
2700 copy = (char *) alloca (p - *argptr + 1);
2701 memcpy (copy, *argptr, p - *argptr);
2702 copy[p - *argptr] = 0;
2703
2704 /* Discard the class name from the arg. */
2705 p = p1 + (p1[0] == ':' ? 2 : 1);
2706 while (*p == ' ' || *p == '\t')
2707 p++;
2708 *argptr = p;
2709
2710 sym_class = lookup_symbol (copy, 0, STRUCT_NAMESPACE, 0,
2711 (struct symtab **) NULL);
2712
2713 if (sym_class &&
2714 (t = check_typedef (SYMBOL_TYPE (sym_class)),
2715 (TYPE_CODE (t) == TYPE_CODE_STRUCT
2716 || TYPE_CODE (t) == TYPE_CODE_UNION)))
2717 {
2718 /* Arg token is not digits => try it as a function name
2719 Find the next token(everything up to end or next blank). */
2720 if (**argptr
2721 && strchr (gdb_completer_quote_characters, **argptr) != NULL)
2722 {
2723 p = skip_quoted (*argptr);
2724 *argptr = *argptr + 1;
2725 }
2726 else
2727 {
2728 p = *argptr;
2729 while (*p && *p != ' ' && *p != '\t' && *p != ',' && *p != ':')
2730 p++;
2731 }
2732 /*
2733 q = operator_chars (*argptr, &q1);
2734 if (q1 - q)
2735 {
2736 char *opname;
2737 char *tmp = alloca (q1 - q + 1);
2738 memcpy (tmp, q, q1 - q);
2739 tmp[q1 - q] = '\0';
2740 opname = cplus_mangle_opname (tmp, DMGL_ANSI);
2741 if (opname == NULL)
2742 {
2743 error_begin ();
2744 printf_filtered ("no mangling for \"%s\"\n", tmp);
2745 cplusplus_hint (saved_arg);
2746 return_to_top_level (RETURN_ERROR);
2747 }
2748 copy = (char*) alloca (3 + strlen(opname));
2749 sprintf (copy, "__%s", opname);
2750 p = q1;
2751 }
2752 else
2753 */
2754 {
2755 copy = (char *) alloca (p - *argptr + 1);
2756 memcpy (copy, *argptr, p - *argptr);
2757 copy[p - *argptr] = '\0';
2758 if (p != *argptr
2759 && copy[p - *argptr - 1]
2760 && strchr (gdb_completer_quote_characters,
2761 copy[p - *argptr - 1]) != NULL)
2762 copy[p - *argptr - 1] = '\0';
2763 }
2764
2765 /* no line number may be specified */
2766 while (*p == ' ' || *p == '\t')
2767 p++;
2768 *argptr = p;
2769
2770 sym = 0;
2771 i1 = 0; /* counter for the symbol array */
2772 sym_arr = (struct symbol **) alloca (total_number_of_methods (t)
2773 * sizeof (struct symbol *));
2774
2775 if (destructor_name_p (copy, t))
2776 {
2777 /* Destructors are a special case. */
2778 int m_index, f_index;
2779
2780 if (get_destructor_fn_field (t, &m_index, &f_index))
2781 {
2782 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, m_index);
2783
2784 sym_arr[i1] =
2785 lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, f_index),
2786 NULL, VAR_NAMESPACE, (int *) NULL,
2787 (struct symtab **) NULL);
2788 if (sym_arr[i1])
2789 i1++;
2790 }
2791 }
2792 else
2793 i1 = find_methods (t, copy, sym_arr);
2794 if (i1 == 1)
2795 {
2796 /* There is exactly one field with that name. */
2797 sym = sym_arr[0];
2798
2799 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2800 {
2801 values.sals = (struct symtab_and_line *)
2802 xmalloc (sizeof (struct symtab_and_line));
2803 values.nelts = 1;
2804 values.sals[0] = find_function_start_sal (sym,
2805 funfirstline);
2806 }
2807 else
2808 {
2809 values.nelts = 0;
2810 }
2811 return values;
2812 }
2813 if (i1 > 0)
2814 {
2815 /* There is more than one field with that name
2816 (overloaded). Ask the user which one to use. */
2817 return decode_line_2 (sym_arr, i1, funfirstline, canonical);
2818 }
2819 else
2820 {
2821 char *tmp;
2822
2823 if (OPNAME_PREFIX_P (copy))
2824 {
2825 tmp = (char *) alloca (strlen (copy + 3) + 9);
2826 strcpy (tmp, "operator ");
2827 strcat (tmp, copy + 3);
2828 }
2829 else
2830 tmp = copy;
2831 error_begin ();
2832 if (tmp[0] == '~')
2833 printf_filtered
2834 ("the class `%s' does not have destructor defined\n",
2835 SYMBOL_SOURCE_NAME (sym_class));
2836 else
2837 printf_filtered
2838 ("the class %s does not have any method named %s\n",
2839 SYMBOL_SOURCE_NAME (sym_class), tmp);
2840 cplusplus_hint (saved_arg);
2841 return_to_top_level (RETURN_ERROR);
2842 }
2843 }
2844
2845 /* Move pointer up to next possible class/namespace token */
2846 p = p2 + 1; /* restart with old value +1 */
2847 /* Move pointer ahead to next double-colon */
2848 while (*p && (p[0] != ' ') && (p[0] != '\t') && (p[0] != '\''))
2849 {
2850 if (p[0] == '<')
2851 {
2852 temp_end = find_template_name_end (p);
2853 if (!temp_end)
2854 error ("malformed template specification in command");
2855 p = temp_end;
2856 }
2857 else if ((p[0] == ':') && (p[1] == ':'))
2858 break; /* found double-colon */
2859 else
2860 p++;
2861 }
2862
2863 if (*p != ':')
2864 break; /* out of the while (1) */
2865
2866 p2 = p; /* save restart for next time around */
2867 *argptr = saved_arg2; /* restore argptr */
2868 } /* while (1) */
2869
2870 /* Last chance attempt -- check entire name as a symbol */
2871 /* Use "copy" in preparation for jumping out of this block,
2872 to be consistent with usage following the jump target */
2873 copy = (char *) alloca (p - saved_arg2 + 1);
2874 memcpy (copy, saved_arg2, p - saved_arg2);
2875 /* Note: if is_quoted should be true, we snuff out quote here anyway */
2876 copy[p - saved_arg2] = '\000';
2877 /* Set argptr to skip over the name */
2878 *argptr = (*p == '\'') ? p + 1 : p;
2879 /* Look up entire name */
2880 sym = lookup_symbol (copy, 0, VAR_NAMESPACE, 0, &sym_symtab);
2881 s = (struct symtab *) 0;
2882 /* Prepare to jump: restore the " if (condition)" so outer layers see it */
2883 /* Symbol was found --> jump to normal symbol processing.
2884 Code following "symbol_found" expects "copy" to have the
2885 symbol name, "sym" to have the symbol pointer, "s" to be
2886 a specified file's symtab, and sym_symtab to be the symbol's
2887 symtab. */
2888 /* By jumping there we avoid falling through the FILE:LINE and
2889 FILE:FUNC processing stuff below */
2890 if (sym)
2891 goto symbol_found;
2892
2893 /* Couldn't find any interpretation as classes/namespaces, so give up */
2894 error_begin ();
2895 /* The quotes are important if copy is empty. */
2896 printf_filtered
2897 ("Can't find member of namespace, class, struct, or union named \"%s\"\n", copy);
2898 cplusplus_hint (saved_arg);
2899 return_to_top_level (RETURN_ERROR);
2900 }
2901 /* end of C++ */
2902
2903
2904 /* Extract the file name. */
2905 p1 = p;
2906 while (p != *argptr && p[-1] == ' ')
2907 --p;
2908 if ((*p == '"') && is_quote_enclosed)
2909 --p;
2910 copy = (char *) alloca (p - *argptr + 1);
2911 if ((**argptr == '"') && is_quote_enclosed)
2912 {
2913 memcpy (copy, *argptr + 1, p - *argptr - 1);
2914 /* It may have the ending quote right after the file name */
2915 if (copy[p - *argptr - 2] == '"')
2916 copy[p - *argptr - 2] = 0;
2917 else
2918 copy[p - *argptr - 1] = 0;
2919 }
2920 else
2921 {
2922 memcpy (copy, *argptr, p - *argptr);
2923 copy[p - *argptr] = 0;
2924 }
2925
2926 /* Find that file's data. */
2927 s = lookup_symtab (copy);
2928 if (s == 0)
2929 {
2930 if (!have_full_symbols () && !have_partial_symbols ())
2931 error (no_symtab_msg);
2932 error ("No source file named %s.", copy);
2933 }
2934
2935 /* Discard the file name from the arg. */
2936 p = p1 + 1;
2937 while (*p == ' ' || *p == '\t')
2938 p++;
2939 *argptr = p;
2940 }
2941 #if 0
2942 /* No one really seems to know why this was added. It certainly
2943 breaks the command line, though, whenever the passed
2944 name is of the form ClassName::Method. This bit of code
2945 singles out the class name, and if funfirstline is set (for
2946 example, you are setting a breakpoint at this function),
2947 you get an error. This did not occur with earlier
2948 verions, so I am ifdef'ing this out. 3/29/99 */
2949 else
2950 {
2951 /* Check if what we have till now is a symbol name */
2952
2953 /* We may be looking at a template instantiation such
2954 as "foo<int>". Check here whether we know about it,
2955 instead of falling through to the code below which
2956 handles ordinary function names, because that code
2957 doesn't like seeing '<' and '>' in a name -- the
2958 skip_quoted call doesn't go past them. So see if we
2959 can figure it out right now. */
2960
2961 copy = (char *) alloca (p - *argptr + 1);
2962 memcpy (copy, *argptr, p - *argptr);
2963 copy[p - *argptr] = '\000';
2964 sym = lookup_symbol (copy, 0, VAR_NAMESPACE, 0, &sym_symtab);
2965 if (sym)
2966 {
2967 /* Yes, we have a symbol; jump to symbol processing */
2968 /* Code after symbol_found expects S, SYM_SYMTAB, SYM,
2969 and COPY to be set correctly */
2970 *argptr = (*p == '\'') ? p + 1 : p;
2971 s = (struct symtab *) 0;
2972 goto symbol_found;
2973 }
2974 /* Otherwise fall out from here and go to file/line spec
2975 processing, etc. */
2976 }
2977 #endif
2978
2979 /* S is specified file's symtab, or 0 if no file specified.
2980 arg no longer contains the file name. */
2981
2982 /* Check whether arg is all digits (and sign) */
2983
2984 q = *argptr;
2985 if (*q == '-' || *q == '+')
2986 q++;
2987 while (*q >= '0' && *q <= '9')
2988 q++;
2989
2990 if (q != *argptr && (*q == 0 || *q == ' ' || *q == '\t' || *q == ','))
2991 {
2992 /* We found a token consisting of all digits -- at least one digit. */
2993 enum sign
2994 {
2995 none, plus, minus
2996 }
2997 sign = none;
2998
2999 /* We might need a canonical line spec if no file was specified. */
3000 int need_canonical = (s == 0) ? 1 : 0;
3001
3002 /* This is where we need to make sure that we have good defaults.
3003 We must guarantee that this section of code is never executed
3004 when we are called with just a function name, since
3005 select_source_symtab calls us with such an argument */
3006
3007 if (s == 0 && default_symtab == 0)
3008 {
3009 select_source_symtab (0);
3010 default_symtab = current_source_symtab;
3011 default_line = current_source_line;
3012 }
3013
3014 if (**argptr == '+')
3015 sign = plus, (*argptr)++;
3016 else if (**argptr == '-')
3017 sign = minus, (*argptr)++;
3018 val.line = atoi (*argptr);
3019 switch (sign)
3020 {
3021 case plus:
3022 if (q == *argptr)
3023 val.line = 5;
3024 if (s == 0)
3025 val.line = default_line + val.line;
3026 break;
3027 case minus:
3028 if (q == *argptr)
3029 val.line = 15;
3030 if (s == 0)
3031 val.line = default_line - val.line;
3032 else
3033 val.line = 1;
3034 break;
3035 case none:
3036 break; /* No need to adjust val.line. */
3037 }
3038
3039 while (*q == ' ' || *q == '\t')
3040 q++;
3041 *argptr = q;
3042 if (s == 0)
3043 s = default_symtab;
3044
3045 /* It is possible that this source file has more than one symtab,
3046 and that the new line number specification has moved us from the
3047 default (in s) to a new one. */
3048 val.symtab = find_line_symtab (s, val.line, NULL, NULL);
3049 if (val.symtab == 0)
3050 val.symtab = s;
3051
3052 val.pc = 0;
3053 values.sals = (struct symtab_and_line *)
3054 xmalloc (sizeof (struct symtab_and_line));
3055 values.sals[0] = val;
3056 values.nelts = 1;
3057 if (need_canonical)
3058 build_canonical_line_spec (values.sals, NULL, canonical);
3059 return values;
3060 }
3061
3062 /* Arg token is not digits => try it as a variable name
3063 Find the next token (everything up to end or next whitespace). */
3064
3065 if (**argptr == '$') /* May be a convenience variable */
3066 p = skip_quoted (*argptr + (((*argptr)[1] == '$') ? 2 : 1)); /* One or two $ chars possible */
3067 else if (is_quoted)
3068 {
3069 p = skip_quoted (*argptr);
3070 if (p[-1] != '\'')
3071 error ("Unmatched single quote.");
3072 }
3073 else if (has_parens)
3074 {
3075 p = pp + 1;
3076 }
3077 else
3078 {
3079 p = skip_quoted (*argptr);
3080 }
3081
3082 if (is_quote_enclosed && **argptr == '"')
3083 (*argptr)++;
3084
3085 copy = (char *) alloca (p - *argptr + 1);
3086 memcpy (copy, *argptr, p - *argptr);
3087 copy[p - *argptr] = '\0';
3088 if (p != *argptr
3089 && copy[0]
3090 && copy[0] == copy[p - *argptr - 1]
3091 && strchr (gdb_completer_quote_characters, copy[0]) != NULL)
3092 {
3093 copy[p - *argptr - 1] = '\0';
3094 copy++;
3095 }
3096 while (*p == ' ' || *p == '\t')
3097 p++;
3098 *argptr = p;
3099
3100 /* If it starts with $: may be a legitimate variable or routine name
3101 (e.g. HP-UX millicode routines such as $$dyncall), or it may
3102 be history value, or it may be a convenience variable */
3103
3104 if (*copy == '$')
3105 {
3106 value_ptr valx;
3107 int index = 0;
3108 int need_canonical = 0;
3109
3110 p = (copy[1] == '$') ? copy + 2 : copy + 1;
3111 while (*p >= '0' && *p <= '9')
3112 p++;
3113 if (!*p) /* reached end of token without hitting non-digit */
3114 {
3115 /* We have a value history reference */
3116 sscanf ((copy[1] == '$') ? copy + 2 : copy + 1, "%d", &index);
3117 valx = access_value_history ((copy[1] == '$') ? -index : index);
3118 if (TYPE_CODE (VALUE_TYPE (valx)) != TYPE_CODE_INT)
3119 error ("History values used in line specs must have integer values.");
3120 }
3121 else
3122 {
3123 /* Not all digits -- may be user variable/function or a
3124 convenience variable */
3125
3126 /* Look up entire name as a symbol first */
3127 sym = lookup_symbol (copy, 0, VAR_NAMESPACE, 0, &sym_symtab);
3128 s = (struct symtab *) 0;
3129 need_canonical = 1;
3130 /* Symbol was found --> jump to normal symbol processing.
3131 Code following "symbol_found" expects "copy" to have the
3132 symbol name, "sym" to have the symbol pointer, "s" to be
3133 a specified file's symtab, and sym_symtab to be the symbol's
3134 symtab. */
3135 if (sym)
3136 goto symbol_found;
3137
3138 /* If symbol was not found, look in minimal symbol tables */
3139 msymbol = lookup_minimal_symbol (copy, 0, 0);
3140 /* Min symbol was found --> jump to minsym processing. */
3141 if (msymbol)
3142 goto minimal_symbol_found;
3143
3144 /* Not a user variable or function -- must be convenience variable */
3145 need_canonical = (s == 0) ? 1 : 0;
3146 valx = value_of_internalvar (lookup_internalvar (copy + 1));
3147 if (TYPE_CODE (VALUE_TYPE (valx)) != TYPE_CODE_INT)
3148 error ("Convenience variables used in line specs must have integer values.");
3149 }
3150
3151 /* Either history value or convenience value from above, in valx */
3152 val.symtab = s ? s : default_symtab;
3153 val.line = value_as_long (valx);
3154 val.pc = 0;
3155
3156 values.sals = (struct symtab_and_line *) xmalloc (sizeof val);
3157 values.sals[0] = val;
3158 values.nelts = 1;
3159
3160 if (need_canonical)
3161 build_canonical_line_spec (values.sals, NULL, canonical);
3162
3163 return values;
3164 }
3165
3166
3167 /* Look up that token as a variable.
3168 If file specified, use that file's per-file block to start with. */
3169
3170 sym = lookup_symbol (copy,
3171 (s ? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK)
3172 : get_selected_block ()),
3173 VAR_NAMESPACE, 0, &sym_symtab);
3174
3175 symbol_found: /* We also jump here from inside the C++ class/namespace
3176 code on finding a symbol of the form "A::B::C" */
3177
3178 if (sym != NULL)
3179 {
3180 if (SYMBOL_CLASS (sym) == LOC_BLOCK)
3181 {
3182 /* Arg is the name of a function */
3183 values.sals = (struct symtab_and_line *)
3184 xmalloc (sizeof (struct symtab_and_line));
3185 values.sals[0] = find_function_start_sal (sym, funfirstline);
3186 values.nelts = 1;
3187
3188 /* Don't use the SYMBOL_LINE; if used at all it points to
3189 the line containing the parameters or thereabouts, not
3190 the first line of code. */
3191
3192 /* We might need a canonical line spec if it is a static
3193 function. */
3194 if (s == 0)
3195 {
3196 struct blockvector *bv = BLOCKVECTOR (sym_symtab);
3197 struct block *b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
3198 if (lookup_block_symbol (b, copy, VAR_NAMESPACE) != NULL)
3199 build_canonical_line_spec (values.sals, copy, canonical);
3200 }
3201 return values;
3202 }
3203 else
3204 {
3205 if (funfirstline)
3206 error ("\"%s\" is not a function", copy);
3207 else if (SYMBOL_LINE (sym) != 0)
3208 {
3209 /* We know its line number. */
3210 values.sals = (struct symtab_and_line *)
3211 xmalloc (sizeof (struct symtab_and_line));
3212 values.nelts = 1;
3213 memset (&values.sals[0], 0, sizeof (values.sals[0]));
3214 values.sals[0].symtab = sym_symtab;
3215 values.sals[0].line = SYMBOL_LINE (sym);
3216 return values;
3217 }
3218 else
3219 /* This can happen if it is compiled with a compiler which doesn't
3220 put out line numbers for variables. */
3221 /* FIXME: Shouldn't we just set .line and .symtab to zero
3222 and return? For example, "info line foo" could print
3223 the address. */
3224 error ("Line number not known for symbol \"%s\"", copy);
3225 }
3226 }
3227
3228 msymbol = lookup_minimal_symbol (copy, NULL, NULL);
3229
3230 minimal_symbol_found: /* We also jump here from the case for variables
3231 that begin with '$' */
3232
3233 if (msymbol != NULL)
3234 {
3235 values.sals = (struct symtab_and_line *)
3236 xmalloc (sizeof (struct symtab_and_line));
3237 values.sals[0] = find_pc_sect_line (SYMBOL_VALUE_ADDRESS (msymbol),
3238 (struct sec *) 0, 0);
3239 values.sals[0].section = SYMBOL_BFD_SECTION (msymbol);
3240 if (funfirstline)
3241 {
3242 values.sals[0].pc += FUNCTION_START_OFFSET;
3243 values.sals[0].pc = SKIP_PROLOGUE (values.sals[0].pc);
3244 }
3245 values.nelts = 1;
3246 return values;
3247 }
3248
3249 if (!have_full_symbols () &&
3250 !have_partial_symbols () && !have_minimal_symbols ())
3251 error (no_symtab_msg);
3252
3253 error ("Function \"%s\" not defined.", copy);
3254 return values; /* for lint */
3255 }
3256
3257 struct symtabs_and_lines
3258 decode_line_spec (char *string, int funfirstline)
3259 {
3260 struct symtabs_and_lines sals;
3261 if (string == 0)
3262 error ("Empty line specification.");
3263 sals = decode_line_1 (&string, funfirstline,
3264 current_source_symtab, current_source_line,
3265 (char ***) NULL);
3266 if (*string)
3267 error ("Junk at end of line specification: %s", string);
3268 return sals;
3269 }
3270
3271 /* Given a list of NELTS symbols in SYM_ARR, return a list of lines to
3272 operate on (ask user if necessary).
3273 If CANONICAL is non-NULL return a corresponding array of mangled names
3274 as canonical line specs there. */
3275
3276 static struct symtabs_and_lines
3277 decode_line_2 (sym_arr, nelts, funfirstline, canonical)
3278 struct symbol *sym_arr[];
3279 int nelts;
3280 int funfirstline;
3281 char ***canonical;
3282 {
3283 struct symtabs_and_lines values, return_values;
3284 char *args, *arg1;
3285 int i;
3286 char *prompt;
3287 char *symname;
3288 struct cleanup *old_chain;
3289 char **canonical_arr = (char **) NULL;
3290
3291 values.sals = (struct symtab_and_line *)
3292 alloca (nelts * sizeof (struct symtab_and_line));
3293 return_values.sals = (struct symtab_and_line *)
3294 xmalloc (nelts * sizeof (struct symtab_and_line));
3295 old_chain = make_cleanup (free, return_values.sals);
3296
3297 if (canonical)
3298 {
3299 canonical_arr = (char **) xmalloc (nelts * sizeof (char *));
3300 make_cleanup (free, canonical_arr);
3301 memset (canonical_arr, 0, nelts * sizeof (char *));
3302 *canonical = canonical_arr;
3303 }
3304
3305 i = 0;
3306 printf_unfiltered ("[0] cancel\n[1] all\n");
3307 while (i < nelts)
3308 {
3309 INIT_SAL (&return_values.sals[i]); /* initialize to zeroes */
3310 INIT_SAL (&values.sals[i]);
3311 if (sym_arr[i] && SYMBOL_CLASS (sym_arr[i]) == LOC_BLOCK)
3312 {
3313 values.sals[i] = find_function_start_sal (sym_arr[i], funfirstline);
3314 printf_unfiltered ("[%d] %s at %s:%d\n",
3315 (i + 2),
3316 SYMBOL_SOURCE_NAME (sym_arr[i]),
3317 values.sals[i].symtab->filename,
3318 values.sals[i].line);
3319 }
3320 else
3321 printf_unfiltered ("?HERE\n");
3322 i++;
3323 }
3324
3325 if ((prompt = getenv ("PS2")) == NULL)
3326 {
3327 prompt = "> ";
3328 }
3329 args = command_line_input (prompt, 0, "overload-choice");
3330
3331 if (args == 0 || *args == 0)
3332 error_no_arg ("one or more choice numbers");
3333
3334 i = 0;
3335 while (*args)
3336 {
3337 int num;
3338
3339 arg1 = args;
3340 while (*arg1 >= '0' && *arg1 <= '9')
3341 arg1++;
3342 if (*arg1 && *arg1 != ' ' && *arg1 != '\t')
3343 error ("Arguments must be choice numbers.");
3344
3345 num = atoi (args);
3346
3347 if (num == 0)
3348 error ("cancelled");
3349 else if (num == 1)
3350 {
3351 if (canonical_arr)
3352 {
3353 for (i = 0; i < nelts; i++)
3354 {
3355 if (canonical_arr[i] == NULL)
3356 {
3357 symname = SYMBOL_NAME (sym_arr[i]);
3358 canonical_arr[i] = savestring (symname, strlen (symname));
3359 }
3360 }
3361 }
3362 memcpy (return_values.sals, values.sals,
3363 (nelts * sizeof (struct symtab_and_line)));
3364 return_values.nelts = nelts;
3365 discard_cleanups (old_chain);
3366 return return_values;
3367 }
3368
3369 if (num >= nelts + 2)
3370 {
3371 printf_unfiltered ("No choice number %d.\n", num);
3372 }
3373 else
3374 {
3375 num -= 2;
3376 if (values.sals[num].pc)
3377 {
3378 if (canonical_arr)
3379 {
3380 symname = SYMBOL_NAME (sym_arr[num]);
3381 make_cleanup (free, symname);
3382 canonical_arr[i] = savestring (symname, strlen (symname));
3383 }
3384 return_values.sals[i++] = values.sals[num];
3385 values.sals[num].pc = 0;
3386 }
3387 else
3388 {
3389 printf_unfiltered ("duplicate request for %d ignored.\n", num);
3390 }
3391 }
3392
3393 args = arg1;
3394 while (*args == ' ' || *args == '\t')
3395 args++;
3396 }
3397 return_values.nelts = i;
3398 discard_cleanups (old_chain);
3399 return return_values;
3400 }
3401 \f
3402
3403 /* Slave routine for sources_info. Force line breaks at ,'s.
3404 NAME is the name to print and *FIRST is nonzero if this is the first
3405 name printed. Set *FIRST to zero. */
3406 static void
3407 output_source_filename (char *name, int *first)
3408 {
3409 /* Table of files printed so far. Since a single source file can
3410 result in several partial symbol tables, we need to avoid printing
3411 it more than once. Note: if some of the psymtabs are read in and
3412 some are not, it gets printed both under "Source files for which
3413 symbols have been read" and "Source files for which symbols will
3414 be read in on demand". I consider this a reasonable way to deal
3415 with the situation. I'm not sure whether this can also happen for
3416 symtabs; it doesn't hurt to check. */
3417 static char **tab = NULL;
3418 /* Allocated size of tab in elements.
3419 Start with one 256-byte block (when using GNU malloc.c).
3420 24 is the malloc overhead when range checking is in effect. */
3421 static int tab_alloc_size = (256 - 24) / sizeof (char *);
3422 /* Current size of tab in elements. */
3423 static int tab_cur_size;
3424
3425 char **p;
3426
3427 if (*first)
3428 {
3429 if (tab == NULL)
3430 tab = (char **) xmalloc (tab_alloc_size * sizeof (*tab));
3431 tab_cur_size = 0;
3432 }
3433
3434 /* Is NAME in tab? */
3435 for (p = tab; p < tab + tab_cur_size; p++)
3436 if (STREQ (*p, name))
3437 /* Yes; don't print it again. */
3438 return;
3439 /* No; add it to tab. */
3440 if (tab_cur_size == tab_alloc_size)
3441 {
3442 tab_alloc_size *= 2;
3443 tab = (char **) xrealloc ((char *) tab, tab_alloc_size * sizeof (*tab));
3444 }
3445 tab[tab_cur_size++] = name;
3446
3447 if (*first)
3448 {
3449 *first = 0;
3450 }
3451 else
3452 {
3453 printf_filtered (", ");
3454 }
3455
3456 wrap_here ("");
3457 fputs_filtered (name, gdb_stdout);
3458 }
3459
3460 static void
3461 sources_info (char *ignore, int from_tty)
3462 {
3463 register struct symtab *s;
3464 register struct partial_symtab *ps;
3465 register struct objfile *objfile;
3466 int first;
3467
3468 if (!have_full_symbols () && !have_partial_symbols ())
3469 {
3470 error (no_symtab_msg);
3471 }
3472
3473 printf_filtered ("Source files for which symbols have been read in:\n\n");
3474
3475 first = 1;
3476 ALL_SYMTABS (objfile, s)
3477 {
3478 output_source_filename (s->filename, &first);
3479 }
3480 printf_filtered ("\n\n");
3481
3482 printf_filtered ("Source files for which symbols will be read in on demand:\n\n");
3483
3484 first = 1;
3485 ALL_PSYMTABS (objfile, ps)
3486 {
3487 if (!ps->readin)
3488 {
3489 output_source_filename (ps->filename, &first);
3490 }
3491 }
3492 printf_filtered ("\n");
3493 }
3494
3495 static int
3496 file_matches (file, files, nfiles)
3497 char *file;
3498 char *files[];
3499 int nfiles;
3500 {
3501 int i;
3502
3503 if (file != NULL && nfiles != 0)
3504 {
3505 for (i = 0; i < nfiles; i++)
3506 {
3507 if (strcmp (files[i], basename (file)) == 0)
3508 return 1;
3509 }
3510 }
3511 else if (nfiles == 0)
3512 return 1;
3513 return 0;
3514 }
3515
3516 /* Free any memory associated with a search. */
3517 void
3518 free_search_symbols (struct symbol_search *symbols)
3519 {
3520 struct symbol_search *p;
3521 struct symbol_search *next;
3522
3523 for (p = symbols; p != NULL; p = next)
3524 {
3525 next = p->next;
3526 free (p);
3527 }
3528 }
3529
3530 static void
3531 do_free_search_symbols_cleanup (void *symbols)
3532 {
3533 free_search_symbols (symbols);
3534 }
3535
3536 struct cleanup *
3537 make_cleanup_free_search_symbols (struct symbol_search *symbols)
3538 {
3539 return make_cleanup (do_free_search_symbols_cleanup, symbols);
3540 }
3541
3542
3543 /* Search the symbol table for matches to the regular expression REGEXP,
3544 returning the results in *MATCHES.
3545
3546 Only symbols of KIND are searched:
3547 FUNCTIONS_NAMESPACE - search all functions
3548 TYPES_NAMESPACE - search all type names
3549 METHODS_NAMESPACE - search all methods NOT IMPLEMENTED
3550 VARIABLES_NAMESPACE - search all symbols, excluding functions, type names,
3551 and constants (enums)
3552
3553 free_search_symbols should be called when *MATCHES is no longer needed.
3554 */
3555 void
3556 search_symbols (regexp, kind, nfiles, files, matches)
3557 char *regexp;
3558 namespace_enum kind;
3559 int nfiles;
3560 char *files[];
3561 struct symbol_search **matches;
3562
3563 {
3564 register struct symtab *s;
3565 register struct partial_symtab *ps;
3566 register struct blockvector *bv;
3567 struct blockvector *prev_bv = 0;
3568 register struct block *b;
3569 register int i = 0;
3570 register int j;
3571 register struct symbol *sym;
3572 struct partial_symbol **psym;
3573 struct objfile *objfile;
3574 struct minimal_symbol *msymbol;
3575 char *val;
3576 int found_misc = 0;
3577 static enum minimal_symbol_type types[]
3578 =
3579 {mst_data, mst_text, mst_abs, mst_unknown};
3580 static enum minimal_symbol_type types2[]
3581 =
3582 {mst_bss, mst_file_text, mst_abs, mst_unknown};
3583 static enum minimal_symbol_type types3[]
3584 =
3585 {mst_file_data, mst_solib_trampoline, mst_abs, mst_unknown};
3586 static enum minimal_symbol_type types4[]
3587 =
3588 {mst_file_bss, mst_text, mst_abs, mst_unknown};
3589 enum minimal_symbol_type ourtype;
3590 enum minimal_symbol_type ourtype2;
3591 enum minimal_symbol_type ourtype3;
3592 enum minimal_symbol_type ourtype4;
3593 struct symbol_search *sr;
3594 struct symbol_search *psr;
3595 struct symbol_search *tail;
3596 struct cleanup *old_chain = NULL;
3597
3598 if (kind < LABEL_NAMESPACE)
3599 error ("must search on specific namespace");
3600
3601 ourtype = types[(int) (kind - VARIABLES_NAMESPACE)];
3602 ourtype2 = types2[(int) (kind - VARIABLES_NAMESPACE)];
3603 ourtype3 = types3[(int) (kind - VARIABLES_NAMESPACE)];
3604 ourtype4 = types4[(int) (kind - VARIABLES_NAMESPACE)];
3605
3606 sr = *matches = NULL;
3607 tail = NULL;
3608
3609 if (regexp != NULL)
3610 {
3611 /* Make sure spacing is right for C++ operators.
3612 This is just a courtesy to make the matching less sensitive
3613 to how many spaces the user leaves between 'operator'
3614 and <TYPENAME> or <OPERATOR>. */
3615 char *opend;
3616 char *opname = operator_chars (regexp, &opend);
3617 if (*opname)
3618 {
3619 int fix = -1; /* -1 means ok; otherwise number of spaces needed. */
3620 if (isalpha (*opname) || *opname == '_' || *opname == '$')
3621 {
3622 /* There should 1 space between 'operator' and 'TYPENAME'. */
3623 if (opname[-1] != ' ' || opname[-2] == ' ')
3624 fix = 1;
3625 }
3626 else
3627 {
3628 /* There should 0 spaces between 'operator' and 'OPERATOR'. */
3629 if (opname[-1] == ' ')
3630 fix = 0;
3631 }
3632 /* If wrong number of spaces, fix it. */
3633 if (fix >= 0)
3634 {
3635 char *tmp = (char *) alloca (opend - opname + 10);
3636 sprintf (tmp, "operator%.*s%s", fix, " ", opname);
3637 regexp = tmp;
3638 }
3639 }
3640
3641 if (0 != (val = re_comp (regexp)))
3642 error ("Invalid regexp (%s): %s", val, regexp);
3643 }
3644
3645 /* Search through the partial symtabs *first* for all symbols
3646 matching the regexp. That way we don't have to reproduce all of
3647 the machinery below. */
3648
3649 ALL_PSYMTABS (objfile, ps)
3650 {
3651 struct partial_symbol **bound, **gbound, **sbound;
3652 int keep_going = 1;
3653
3654 if (ps->readin)
3655 continue;
3656
3657 gbound = objfile->global_psymbols.list + ps->globals_offset + ps->n_global_syms;
3658 sbound = objfile->static_psymbols.list + ps->statics_offset + ps->n_static_syms;
3659 bound = gbound;
3660
3661 /* Go through all of the symbols stored in a partial
3662 symtab in one loop. */
3663 psym = objfile->global_psymbols.list + ps->globals_offset;
3664 while (keep_going)
3665 {
3666 if (psym >= bound)
3667 {
3668 if (bound == gbound && ps->n_static_syms != 0)
3669 {
3670 psym = objfile->static_psymbols.list + ps->statics_offset;
3671 bound = sbound;
3672 }
3673 else
3674 keep_going = 0;
3675 continue;
3676 }
3677 else
3678 {
3679 QUIT;
3680
3681 /* If it would match (logic taken from loop below)
3682 load the file and go on to the next one */
3683 if (file_matches (ps->filename, files, nfiles)
3684 && ((regexp == NULL || SYMBOL_MATCHES_REGEXP (*psym))
3685 && ((kind == VARIABLES_NAMESPACE && SYMBOL_CLASS (*psym) != LOC_TYPEDEF
3686 && SYMBOL_CLASS (*psym) != LOC_BLOCK)
3687 || (kind == FUNCTIONS_NAMESPACE && SYMBOL_CLASS (*psym) == LOC_BLOCK)
3688 || (kind == TYPES_NAMESPACE && SYMBOL_CLASS (*psym) == LOC_TYPEDEF)
3689 || (kind == METHODS_NAMESPACE && SYMBOL_CLASS (*psym) == LOC_BLOCK))))
3690 {
3691 PSYMTAB_TO_SYMTAB (ps);
3692 keep_going = 0;
3693 }
3694 }
3695 psym++;
3696 }
3697 }
3698
3699 /* Here, we search through the minimal symbol tables for functions
3700 and variables that match, and force their symbols to be read.
3701 This is in particular necessary for demangled variable names,
3702 which are no longer put into the partial symbol tables.
3703 The symbol will then be found during the scan of symtabs below.
3704
3705 For functions, find_pc_symtab should succeed if we have debug info
3706 for the function, for variables we have to call lookup_symbol
3707 to determine if the variable has debug info.
3708 If the lookup fails, set found_misc so that we will rescan to print
3709 any matching symbols without debug info.
3710 */
3711
3712 if (nfiles == 0 && (kind == VARIABLES_NAMESPACE || kind == FUNCTIONS_NAMESPACE))
3713 {
3714 ALL_MSYMBOLS (objfile, msymbol)
3715 {
3716 if (MSYMBOL_TYPE (msymbol) == ourtype ||
3717 MSYMBOL_TYPE (msymbol) == ourtype2 ||
3718 MSYMBOL_TYPE (msymbol) == ourtype3 ||
3719 MSYMBOL_TYPE (msymbol) == ourtype4)
3720 {
3721 if (regexp == NULL || SYMBOL_MATCHES_REGEXP (msymbol))
3722 {
3723 if (0 == find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol)))
3724 {
3725 if (kind == FUNCTIONS_NAMESPACE
3726 || lookup_symbol (SYMBOL_NAME (msymbol),
3727 (struct block *) NULL,
3728 VAR_NAMESPACE,
3729 0, (struct symtab **) NULL) == NULL)
3730 found_misc = 1;
3731 }
3732 }
3733 }
3734 }
3735 }
3736
3737 ALL_SYMTABS (objfile, s)
3738 {
3739 bv = BLOCKVECTOR (s);
3740 /* Often many files share a blockvector.
3741 Scan each blockvector only once so that
3742 we don't get every symbol many times.
3743 It happens that the first symtab in the list
3744 for any given blockvector is the main file. */
3745 if (bv != prev_bv)
3746 for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
3747 {
3748 b = BLOCKVECTOR_BLOCK (bv, i);
3749 /* Skip the sort if this block is always sorted. */
3750 if (!BLOCK_SHOULD_SORT (b))
3751 sort_block_syms (b);
3752 for (j = 0; j < BLOCK_NSYMS (b); j++)
3753 {
3754 QUIT;
3755 sym = BLOCK_SYM (b, j);
3756 if (file_matches (s->filename, files, nfiles)
3757 && ((regexp == NULL || SYMBOL_MATCHES_REGEXP (sym))
3758 && ((kind == VARIABLES_NAMESPACE && SYMBOL_CLASS (sym) != LOC_TYPEDEF
3759 && SYMBOL_CLASS (sym) != LOC_BLOCK
3760 && SYMBOL_CLASS (sym) != LOC_CONST)
3761 || (kind == FUNCTIONS_NAMESPACE && SYMBOL_CLASS (sym) == LOC_BLOCK)
3762 || (kind == TYPES_NAMESPACE && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
3763 || (kind == METHODS_NAMESPACE && SYMBOL_CLASS (sym) == LOC_BLOCK))))
3764 {
3765 /* match */
3766 psr = (struct symbol_search *) xmalloc (sizeof (struct symbol_search));
3767 psr->block = i;
3768 psr->symtab = s;
3769 psr->symbol = sym;
3770 psr->msymbol = NULL;
3771 psr->next = NULL;
3772 if (tail == NULL)
3773 {
3774 sr = psr;
3775 old_chain = make_cleanup_free_search_symbols (sr);
3776 }
3777 else
3778 tail->next = psr;
3779 tail = psr;
3780 }
3781 }
3782 }
3783 prev_bv = bv;
3784 }
3785
3786 /* If there are no eyes, avoid all contact. I mean, if there are
3787 no debug symbols, then print directly from the msymbol_vector. */
3788
3789 if (found_misc || kind != FUNCTIONS_NAMESPACE)
3790 {
3791 ALL_MSYMBOLS (objfile, msymbol)
3792 {
3793 if (MSYMBOL_TYPE (msymbol) == ourtype ||
3794 MSYMBOL_TYPE (msymbol) == ourtype2 ||
3795 MSYMBOL_TYPE (msymbol) == ourtype3 ||
3796 MSYMBOL_TYPE (msymbol) == ourtype4)
3797 {
3798 if (regexp == NULL || SYMBOL_MATCHES_REGEXP (msymbol))
3799 {
3800 /* Functions: Look up by address. */
3801 if (kind != FUNCTIONS_NAMESPACE ||
3802 (0 == find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol))))
3803 {
3804 /* Variables/Absolutes: Look up by name */
3805 if (lookup_symbol (SYMBOL_NAME (msymbol),
3806 (struct block *) NULL, VAR_NAMESPACE,
3807 0, (struct symtab **) NULL) == NULL)
3808 {
3809 /* match */
3810 psr = (struct symbol_search *) xmalloc (sizeof (struct symbol_search));
3811 psr->block = i;
3812 psr->msymbol = msymbol;
3813 psr->symtab = NULL;
3814 psr->symbol = NULL;
3815 psr->next = NULL;
3816 if (tail == NULL)
3817 {
3818 sr = psr;
3819 old_chain = make_cleanup_free_search_symbols (sr);
3820 }
3821 else
3822 tail->next = psr;
3823 tail = psr;
3824 }
3825 }
3826 }
3827 }
3828 }
3829 }
3830
3831 *matches = sr;
3832 if (sr != NULL)
3833 discard_cleanups (old_chain);
3834 }
3835
3836 /* Helper function for symtab_symbol_info, this function uses
3837 the data returned from search_symbols() to print information
3838 regarding the match to gdb_stdout.
3839 */
3840 static void
3841 print_symbol_info (namespace_enum kind, struct symtab *s, struct symbol *sym,
3842 int block, char *last)
3843 {
3844 if (last == NULL || strcmp (last, s->filename) != 0)
3845 {
3846 fputs_filtered ("\nFile ", gdb_stdout);
3847 fputs_filtered (s->filename, gdb_stdout);
3848 fputs_filtered (":\n", gdb_stdout);
3849 }
3850
3851 if (kind != TYPES_NAMESPACE && block == STATIC_BLOCK)
3852 printf_filtered ("static ");
3853
3854 /* Typedef that is not a C++ class */
3855 if (kind == TYPES_NAMESPACE
3856 && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE)
3857 typedef_print (SYMBOL_TYPE (sym), sym, gdb_stdout);
3858 /* variable, func, or typedef-that-is-c++-class */
3859 else if (kind < TYPES_NAMESPACE ||
3860 (kind == TYPES_NAMESPACE &&
3861 SYMBOL_NAMESPACE (sym) == STRUCT_NAMESPACE))
3862 {
3863 type_print (SYMBOL_TYPE (sym),
3864 (SYMBOL_CLASS (sym) == LOC_TYPEDEF
3865 ? "" : SYMBOL_SOURCE_NAME (sym)),
3866 gdb_stdout, 0);
3867
3868 printf_filtered (";\n");
3869 }
3870 else
3871 {
3872 #if 0
3873 /* Tiemann says: "info methods was never implemented." */
3874 char *demangled_name;
3875 c_type_print_base (TYPE_FN_FIELD_TYPE (t, block),
3876 gdb_stdout, 0, 0);
3877 c_type_print_varspec_prefix (TYPE_FN_FIELD_TYPE (t, block),
3878 gdb_stdout, 0);
3879 if (TYPE_FN_FIELD_STUB (t, block))
3880 check_stub_method (TYPE_DOMAIN_TYPE (type), j, block);
3881 demangled_name =
3882 cplus_demangle (TYPE_FN_FIELD_PHYSNAME (t, block),
3883 DMGL_ANSI | DMGL_PARAMS);
3884 if (demangled_name == NULL)
3885 fprintf_filtered (stream, "<badly mangled name %s>",
3886 TYPE_FN_FIELD_PHYSNAME (t, block));
3887 else
3888 {
3889 fputs_filtered (demangled_name, stream);
3890 free (demangled_name);
3891 }
3892 #endif
3893 }
3894 }
3895
3896 /* This help function for symtab_symbol_info() prints information
3897 for non-debugging symbols to gdb_stdout.
3898 */
3899 static void
3900 print_msymbol_info (struct minimal_symbol *msymbol)
3901 {
3902 printf_filtered (" %08lx %s\n",
3903 (unsigned long) SYMBOL_VALUE_ADDRESS (msymbol),
3904 SYMBOL_SOURCE_NAME (msymbol));
3905 }
3906
3907 /* This is the guts of the commands "info functions", "info types", and
3908 "info variables". It calls search_symbols to find all matches and then
3909 print_[m]symbol_info to print out some useful information about the
3910 matches.
3911 */
3912 static void
3913 symtab_symbol_info (char *regexp, namespace_enum kind, int from_tty)
3914 {
3915 static char *classnames[]
3916 =
3917 {"variable", "function", "type", "method"};
3918 struct symbol_search *symbols;
3919 struct symbol_search *p;
3920 struct cleanup *old_chain;
3921 char *last_filename = NULL;
3922 int first = 1;
3923
3924 /* must make sure that if we're interrupted, symbols gets freed */
3925 search_symbols (regexp, kind, 0, (char **) NULL, &symbols);
3926 old_chain = make_cleanup_free_search_symbols (symbols);
3927
3928 printf_filtered (regexp
3929 ? "All %ss matching regular expression \"%s\":\n"
3930 : "All defined %ss:\n",
3931 classnames[(int) (kind - VARIABLES_NAMESPACE)], regexp);
3932
3933 for (p = symbols; p != NULL; p = p->next)
3934 {
3935 QUIT;
3936
3937 if (p->msymbol != NULL)
3938 {
3939 if (first)
3940 {
3941 printf_filtered ("\nNon-debugging symbols:\n");
3942 first = 0;
3943 }
3944 print_msymbol_info (p->msymbol);
3945 }
3946 else
3947 {
3948 print_symbol_info (kind,
3949 p->symtab,
3950 p->symbol,
3951 p->block,
3952 last_filename);
3953 last_filename = p->symtab->filename;
3954 }
3955 }
3956
3957 do_cleanups (old_chain);
3958 }
3959
3960 static void
3961 variables_info (char *regexp, int from_tty)
3962 {
3963 symtab_symbol_info (regexp, VARIABLES_NAMESPACE, from_tty);
3964 }
3965
3966 static void
3967 functions_info (char *regexp, int from_tty)
3968 {
3969 symtab_symbol_info (regexp, FUNCTIONS_NAMESPACE, from_tty);
3970 }
3971
3972
3973 static void
3974 types_info (char *regexp, int from_tty)
3975 {
3976 symtab_symbol_info (regexp, TYPES_NAMESPACE, from_tty);
3977 }
3978
3979 #if 0
3980 /* Tiemann says: "info methods was never implemented." */
3981 static void
3982 methods_info (char *regexp)
3983 {
3984 symtab_symbol_info (regexp, METHODS_NAMESPACE, 0, from_tty);
3985 }
3986 #endif /* 0 */
3987
3988 /* Breakpoint all functions matching regular expression. */
3989 #ifdef UI_OUT
3990 void
3991 rbreak_command_wrapper (char *regexp, int from_tty)
3992 {
3993 rbreak_command (regexp, from_tty);
3994 }
3995 #endif
3996 static void
3997 rbreak_command (char *regexp, int from_tty)
3998 {
3999 struct symbol_search *ss;
4000 struct symbol_search *p;
4001 struct cleanup *old_chain;
4002
4003 search_symbols (regexp, FUNCTIONS_NAMESPACE, 0, (char **) NULL, &ss);
4004 old_chain = make_cleanup_free_search_symbols (ss);
4005
4006 for (p = ss; p != NULL; p = p->next)
4007 {
4008 if (p->msymbol == NULL)
4009 {
4010 char *string = (char *) alloca (strlen (p->symtab->filename)
4011 + strlen (SYMBOL_NAME (p->symbol))
4012 + 4);
4013 strcpy (string, p->symtab->filename);
4014 strcat (string, ":'");
4015 strcat (string, SYMBOL_NAME (p->symbol));
4016 strcat (string, "'");
4017 break_command (string, from_tty);
4018 print_symbol_info (FUNCTIONS_NAMESPACE,
4019 p->symtab,
4020 p->symbol,
4021 p->block,
4022 p->symtab->filename);
4023 }
4024 else
4025 {
4026 break_command (SYMBOL_NAME (p->msymbol), from_tty);
4027 printf_filtered ("<function, no debug info> %s;\n",
4028 SYMBOL_SOURCE_NAME (p->msymbol));
4029 }
4030 }
4031
4032 do_cleanups (old_chain);
4033 }
4034 \f
4035
4036 /* Return Nonzero if block a is lexically nested within block b,
4037 or if a and b have the same pc range.
4038 Return zero otherwise. */
4039 int
4040 contained_in (struct block *a, struct block *b)
4041 {
4042 if (!a || !b)
4043 return 0;
4044 return BLOCK_START (a) >= BLOCK_START (b)
4045 && BLOCK_END (a) <= BLOCK_END (b);
4046 }
4047 \f
4048
4049 /* Helper routine for make_symbol_completion_list. */
4050
4051 static int return_val_size;
4052 static int return_val_index;
4053 static char **return_val;
4054
4055 #define COMPLETION_LIST_ADD_SYMBOL(symbol, sym_text, len, text, word) \
4056 do { \
4057 if (SYMBOL_DEMANGLED_NAME (symbol) != NULL) \
4058 /* Put only the mangled name on the list. */ \
4059 /* Advantage: "b foo<TAB>" completes to "b foo(int, int)" */ \
4060 /* Disadvantage: "b foo__i<TAB>" doesn't complete. */ \
4061 completion_list_add_name \
4062 (SYMBOL_DEMANGLED_NAME (symbol), (sym_text), (len), (text), (word)); \
4063 else \
4064 completion_list_add_name \
4065 (SYMBOL_NAME (symbol), (sym_text), (len), (text), (word)); \
4066 } while (0)
4067
4068 /* Test to see if the symbol specified by SYMNAME (which is already
4069 demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN
4070 characters. If so, add it to the current completion list. */
4071
4072 static void
4073 completion_list_add_name (char *symname, char *sym_text, int sym_text_len,
4074 char *text, char *word)
4075 {
4076 int newsize;
4077 int i;
4078
4079 /* clip symbols that cannot match */
4080
4081 if (strncmp (symname, sym_text, sym_text_len) != 0)
4082 {
4083 return;
4084 }
4085
4086 /* Clip any symbol names that we've already considered. (This is a
4087 time optimization) */
4088
4089 for (i = 0; i < return_val_index; ++i)
4090 {
4091 if (STREQ (symname, return_val[i]))
4092 {
4093 return;
4094 }
4095 }
4096
4097 /* We have a match for a completion, so add SYMNAME to the current list
4098 of matches. Note that the name is moved to freshly malloc'd space. */
4099
4100 {
4101 char *new;
4102 if (word == sym_text)
4103 {
4104 new = xmalloc (strlen (symname) + 5);
4105 strcpy (new, symname);
4106 }
4107 else if (word > sym_text)
4108 {
4109 /* Return some portion of symname. */
4110 new = xmalloc (strlen (symname) + 5);
4111 strcpy (new, symname + (word - sym_text));
4112 }
4113 else
4114 {
4115 /* Return some of SYM_TEXT plus symname. */
4116 new = xmalloc (strlen (symname) + (sym_text - word) + 5);
4117 strncpy (new, word, sym_text - word);
4118 new[sym_text - word] = '\0';
4119 strcat (new, symname);
4120 }
4121
4122 /* Recheck for duplicates if we intend to add a modified symbol. */
4123 if (word != sym_text)
4124 {
4125 for (i = 0; i < return_val_index; ++i)
4126 {
4127 if (STREQ (new, return_val[i]))
4128 {
4129 free (new);
4130 return;
4131 }
4132 }
4133 }
4134
4135 if (return_val_index + 3 > return_val_size)
4136 {
4137 newsize = (return_val_size *= 2) * sizeof (char *);
4138 return_val = (char **) xrealloc ((char *) return_val, newsize);
4139 }
4140 return_val[return_val_index++] = new;
4141 return_val[return_val_index] = NULL;
4142 }
4143 }
4144
4145 /* Return a NULL terminated array of all symbols (regardless of class) which
4146 begin by matching TEXT. If the answer is no symbols, then the return value
4147 is an array which contains only a NULL pointer.
4148
4149 Problem: All of the symbols have to be copied because readline frees them.
4150 I'm not going to worry about this; hopefully there won't be that many. */
4151
4152 char **
4153 make_symbol_completion_list (char *text, char *word)
4154 {
4155 register struct symbol *sym;
4156 register struct symtab *s;
4157 register struct partial_symtab *ps;
4158 register struct minimal_symbol *msymbol;
4159 register struct objfile *objfile;
4160 register struct block *b, *surrounding_static_block = 0;
4161 register int i, j;
4162 struct partial_symbol **psym;
4163 /* The symbol we are completing on. Points in same buffer as text. */
4164 char *sym_text;
4165 /* Length of sym_text. */
4166 int sym_text_len;
4167
4168 /* Now look for the symbol we are supposed to complete on.
4169 FIXME: This should be language-specific. */
4170 {
4171 char *p;
4172 char quote_found;
4173 char *quote_pos = NULL;
4174
4175 /* First see if this is a quoted string. */
4176 quote_found = '\0';
4177 for (p = text; *p != '\0'; ++p)
4178 {
4179 if (quote_found != '\0')
4180 {
4181 if (*p == quote_found)
4182 /* Found close quote. */
4183 quote_found = '\0';
4184 else if (*p == '\\' && p[1] == quote_found)
4185 /* A backslash followed by the quote character
4186 doesn't end the string. */
4187 ++p;
4188 }
4189 else if (*p == '\'' || *p == '"')
4190 {
4191 quote_found = *p;
4192 quote_pos = p;
4193 }
4194 }
4195 if (quote_found == '\'')
4196 /* A string within single quotes can be a symbol, so complete on it. */
4197 sym_text = quote_pos + 1;
4198 else if (quote_found == '"')
4199 /* A double-quoted string is never a symbol, nor does it make sense
4200 to complete it any other way. */
4201 return NULL;
4202 else
4203 {
4204 /* It is not a quoted string. Break it based on the characters
4205 which are in symbols. */
4206 while (p > text)
4207 {
4208 if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0')
4209 --p;
4210 else
4211 break;
4212 }
4213 sym_text = p;
4214 }
4215 }
4216
4217 sym_text_len = strlen (sym_text);
4218
4219 return_val_size = 100;
4220 return_val_index = 0;
4221 return_val = (char **) xmalloc ((return_val_size + 1) * sizeof (char *));
4222 return_val[0] = NULL;
4223
4224 /* Look through the partial symtabs for all symbols which begin
4225 by matching SYM_TEXT. Add each one that you find to the list. */
4226
4227 ALL_PSYMTABS (objfile, ps)
4228 {
4229 /* If the psymtab's been read in we'll get it when we search
4230 through the blockvector. */
4231 if (ps->readin)
4232 continue;
4233
4234 for (psym = objfile->global_psymbols.list + ps->globals_offset;
4235 psym < (objfile->global_psymbols.list + ps->globals_offset
4236 + ps->n_global_syms);
4237 psym++)
4238 {
4239 /* If interrupted, then quit. */
4240 QUIT;
4241 COMPLETION_LIST_ADD_SYMBOL (*psym, sym_text, sym_text_len, text, word);
4242 }
4243
4244 for (psym = objfile->static_psymbols.list + ps->statics_offset;
4245 psym < (objfile->static_psymbols.list + ps->statics_offset
4246 + ps->n_static_syms);
4247 psym++)
4248 {
4249 QUIT;
4250 COMPLETION_LIST_ADD_SYMBOL (*psym, sym_text, sym_text_len, text, word);
4251 }
4252 }
4253
4254 /* At this point scan through the misc symbol vectors and add each
4255 symbol you find to the list. Eventually we want to ignore
4256 anything that isn't a text symbol (everything else will be
4257 handled by the psymtab code above). */
4258
4259 ALL_MSYMBOLS (objfile, msymbol)
4260 {
4261 QUIT;
4262 COMPLETION_LIST_ADD_SYMBOL (msymbol, sym_text, sym_text_len, text, word);
4263 }
4264
4265 /* Search upwards from currently selected frame (so that we can
4266 complete on local vars. */
4267
4268 for (b = get_selected_block (); b != NULL; b = BLOCK_SUPERBLOCK (b))
4269 {
4270 if (!BLOCK_SUPERBLOCK (b))
4271 {
4272 surrounding_static_block = b; /* For elmin of dups */
4273 }
4274
4275 /* Also catch fields of types defined in this places which match our
4276 text string. Only complete on types visible from current context. */
4277
4278 for (i = 0; i < BLOCK_NSYMS (b); i++)
4279 {
4280 sym = BLOCK_SYM (b, i);
4281 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
4282 if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
4283 {
4284 struct type *t = SYMBOL_TYPE (sym);
4285 enum type_code c = TYPE_CODE (t);
4286
4287 if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
4288 {
4289 for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++)
4290 {
4291 if (TYPE_FIELD_NAME (t, j))
4292 {
4293 completion_list_add_name (TYPE_FIELD_NAME (t, j),
4294 sym_text, sym_text_len, text, word);
4295 }
4296 }
4297 }
4298 }
4299 }
4300 }
4301
4302 /* Go through the symtabs and check the externs and statics for
4303 symbols which match. */
4304
4305 ALL_SYMTABS (objfile, s)
4306 {
4307 QUIT;
4308 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
4309 for (i = 0; i < BLOCK_NSYMS (b); i++)
4310 {
4311 sym = BLOCK_SYM (b, i);
4312 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
4313 }
4314 }
4315
4316 ALL_SYMTABS (objfile, s)
4317 {
4318 QUIT;
4319 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
4320 /* Don't do this block twice. */
4321 if (b == surrounding_static_block)
4322 continue;
4323 for (i = 0; i < BLOCK_NSYMS (b); i++)
4324 {
4325 sym = BLOCK_SYM (b, i);
4326 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
4327 }
4328 }
4329
4330 return (return_val);
4331 }
4332
4333 /* Determine if PC is in the prologue of a function. The prologue is the area
4334 between the first instruction of a function, and the first executable line.
4335 Returns 1 if PC *might* be in prologue, 0 if definately *not* in prologue.
4336
4337 If non-zero, func_start is where we think the prologue starts, possibly
4338 by previous examination of symbol table information.
4339 */
4340
4341 int
4342 in_prologue (CORE_ADDR pc, CORE_ADDR func_start)
4343 {
4344 struct symtab_and_line sal;
4345 CORE_ADDR func_addr, func_end;
4346
4347 /* We have several sources of information we can consult to figure
4348 this out.
4349 - Compilers usually emit line number info that marks the prologue
4350 as its own "source line". So the ending address of that "line"
4351 is the end of the prologue. If available, this is the most
4352 reliable method.
4353 - The minimal symbols and partial symbols, which can usually tell
4354 us the starting and ending addresses of a function.
4355 - If we know the function's start address, we can call the
4356 architecture-defined SKIP_PROLOGUE function to analyze the
4357 instruction stream and guess where the prologue ends.
4358 - Our `func_start' argument; if non-zero, this is the caller's
4359 best guess as to the function's entry point. At the time of
4360 this writing, handle_inferior_event doesn't get this right, so
4361 it should be our last resort. */
4362
4363 /* Consult the partial symbol table, to find which function
4364 the PC is in. */
4365 if (! find_pc_partial_function (pc, NULL, &func_addr, &func_end))
4366 {
4367 CORE_ADDR prologue_end;
4368
4369 /* We don't even have minsym information, so fall back to using
4370 func_start, if given. */
4371 if (! func_start)
4372 return 1; /* We *might* be in a prologue. */
4373
4374 prologue_end = SKIP_PROLOGUE (func_start);
4375
4376 return func_start <= pc && pc < prologue_end;
4377 }
4378
4379 /* If we have line number information for the function, that's
4380 usually pretty reliable. */
4381 sal = find_pc_line (func_addr, 0);
4382
4383 /* Now sal describes the source line at the function's entry point,
4384 which (by convention) is the prologue. The end of that "line",
4385 sal.end, is the end of the prologue.
4386
4387 Note that, for functions whose source code is all on a single
4388 line, the line number information doesn't always end up this way.
4389 So we must verify that our purported end-of-prologue address is
4390 *within* the function, not at its start or end. */
4391 if (sal.line == 0
4392 || sal.end <= func_addr
4393 || func_end <= sal.end)
4394 {
4395 /* We don't have any good line number info, so use the minsym
4396 information, together with the architecture-specific prologue
4397 scanning code. */
4398 CORE_ADDR prologue_end = SKIP_PROLOGUE (func_addr);
4399
4400 return func_addr <= pc && pc < prologue_end;
4401 }
4402
4403 /* We have line number info, and it looks good. */
4404 return func_addr <= pc && pc < sal.end;
4405 }
4406
4407
4408 /* Begin overload resolution functions */
4409 /* Helper routine for make_symbol_completion_list. */
4410
4411 static int sym_return_val_size;
4412 static int sym_return_val_index;
4413 static struct symbol **sym_return_val;
4414
4415 /* Test to see if the symbol specified by SYMNAME (which is already
4416 demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN
4417 characters. If so, add it to the current completion list. */
4418
4419 static void
4420 overload_list_add_symbol (struct symbol *sym, char *oload_name)
4421 {
4422 int newsize;
4423 int i;
4424
4425 /* Get the demangled name without parameters */
4426 char *sym_name = cplus_demangle (SYMBOL_NAME (sym), DMGL_ARM | DMGL_ANSI);
4427 if (!sym_name)
4428 {
4429 sym_name = (char *) xmalloc (strlen (SYMBOL_NAME (sym)) + 1);
4430 strcpy (sym_name, SYMBOL_NAME (sym));
4431 }
4432
4433 /* skip symbols that cannot match */
4434 if (strcmp (sym_name, oload_name) != 0)
4435 {
4436 free (sym_name);
4437 return;
4438 }
4439
4440 /* If there is no type information, we can't do anything, so skip */
4441 if (SYMBOL_TYPE (sym) == NULL)
4442 return;
4443
4444 /* skip any symbols that we've already considered. */
4445 for (i = 0; i < sym_return_val_index; ++i)
4446 if (!strcmp (SYMBOL_NAME (sym), SYMBOL_NAME (sym_return_val[i])))
4447 return;
4448
4449 /* We have a match for an overload instance, so add SYM to the current list
4450 * of overload instances */
4451 if (sym_return_val_index + 3 > sym_return_val_size)
4452 {
4453 newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
4454 sym_return_val = (struct symbol **) xrealloc ((char *) sym_return_val, newsize);
4455 }
4456 sym_return_val[sym_return_val_index++] = sym;
4457 sym_return_val[sym_return_val_index] = NULL;
4458
4459 free (sym_name);
4460 }
4461
4462 /* Return a null-terminated list of pointers to function symbols that
4463 * match name of the supplied symbol FSYM.
4464 * This is used in finding all overloaded instances of a function name.
4465 * This has been modified from make_symbol_completion_list. */
4466
4467
4468 struct symbol **
4469 make_symbol_overload_list (struct symbol *fsym)
4470 {
4471 register struct symbol *sym;
4472 register struct symtab *s;
4473 register struct partial_symtab *ps;
4474 register struct objfile *objfile;
4475 register struct block *b, *surrounding_static_block = 0;
4476 register int i;
4477 /* The name we are completing on. */
4478 char *oload_name = NULL;
4479 /* Length of name. */
4480 int oload_name_len = 0;
4481
4482 /* Look for the symbol we are supposed to complete on.
4483 * FIXME: This should be language-specific. */
4484
4485 oload_name = cplus_demangle (SYMBOL_NAME (fsym), DMGL_ARM | DMGL_ANSI);
4486 if (!oload_name)
4487 {
4488 oload_name = (char *) xmalloc (strlen (SYMBOL_NAME (fsym)) + 1);
4489 strcpy (oload_name, SYMBOL_NAME (fsym));
4490 }
4491 oload_name_len = strlen (oload_name);
4492
4493 sym_return_val_size = 100;
4494 sym_return_val_index = 0;
4495 sym_return_val = (struct symbol **) xmalloc ((sym_return_val_size + 1) * sizeof (struct symbol *));
4496 sym_return_val[0] = NULL;
4497
4498 /* Look through the partial symtabs for all symbols which begin
4499 by matching OLOAD_NAME. Make sure we read that symbol table in. */
4500
4501 ALL_PSYMTABS (objfile, ps)
4502 {
4503 struct partial_symbol **psym;
4504
4505 /* If the psymtab's been read in we'll get it when we search
4506 through the blockvector. */
4507 if (ps->readin)
4508 continue;
4509
4510 for (psym = objfile->global_psymbols.list + ps->globals_offset;
4511 psym < (objfile->global_psymbols.list + ps->globals_offset
4512 + ps->n_global_syms);
4513 psym++)
4514 {
4515 /* If interrupted, then quit. */
4516 QUIT;
4517 /* This will cause the symbol table to be read if it has not yet been */
4518 s = PSYMTAB_TO_SYMTAB (ps);
4519 }
4520
4521 for (psym = objfile->static_psymbols.list + ps->statics_offset;
4522 psym < (objfile->static_psymbols.list + ps->statics_offset
4523 + ps->n_static_syms);
4524 psym++)
4525 {
4526 QUIT;
4527 /* This will cause the symbol table to be read if it has not yet been */
4528 s = PSYMTAB_TO_SYMTAB (ps);
4529 }
4530 }
4531
4532 /* Search upwards from currently selected frame (so that we can
4533 complete on local vars. */
4534
4535 for (b = get_selected_block (); b != NULL; b = BLOCK_SUPERBLOCK (b))
4536 {
4537 if (!BLOCK_SUPERBLOCK (b))
4538 {
4539 surrounding_static_block = b; /* For elimination of dups */
4540 }
4541
4542 /* Also catch fields of types defined in this places which match our
4543 text string. Only complete on types visible from current context. */
4544
4545 for (i = 0; i < BLOCK_NSYMS (b); i++)
4546 {
4547 sym = BLOCK_SYM (b, i);
4548 overload_list_add_symbol (sym, oload_name);
4549 }
4550 }
4551
4552 /* Go through the symtabs and check the externs and statics for
4553 symbols which match. */
4554
4555 ALL_SYMTABS (objfile, s)
4556 {
4557 QUIT;
4558 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
4559 for (i = 0; i < BLOCK_NSYMS (b); i++)
4560 {
4561 sym = BLOCK_SYM (b, i);
4562 overload_list_add_symbol (sym, oload_name);
4563 }
4564 }
4565
4566 ALL_SYMTABS (objfile, s)
4567 {
4568 QUIT;
4569 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
4570 /* Don't do this block twice. */
4571 if (b == surrounding_static_block)
4572 continue;
4573 for (i = 0; i < BLOCK_NSYMS (b); i++)
4574 {
4575 sym = BLOCK_SYM (b, i);
4576 overload_list_add_symbol (sym, oload_name);
4577 }
4578 }
4579
4580 free (oload_name);
4581
4582 return (sym_return_val);
4583 }
4584
4585 /* End of overload resolution functions */
4586 \f
4587
4588 void
4589 _initialize_symtab (void)
4590 {
4591 add_info ("variables", variables_info,
4592 "All global and static variable names, or those matching REGEXP.");
4593 if (dbx_commands)
4594 add_com ("whereis", class_info, variables_info,
4595 "All global and static variable names, or those matching REGEXP.");
4596
4597 add_info ("functions", functions_info,
4598 "All function names, or those matching REGEXP.");
4599
4600
4601 /* FIXME: This command has at least the following problems:
4602 1. It prints builtin types (in a very strange and confusing fashion).
4603 2. It doesn't print right, e.g. with
4604 typedef struct foo *FOO
4605 type_print prints "FOO" when we want to make it (in this situation)
4606 print "struct foo *".
4607 I also think "ptype" or "whatis" is more likely to be useful (but if
4608 there is much disagreement "info types" can be fixed). */
4609 add_info ("types", types_info,
4610 "All type names, or those matching REGEXP.");
4611
4612 #if 0
4613 add_info ("methods", methods_info,
4614 "All method names, or those matching REGEXP::REGEXP.\n\
4615 If the class qualifier is omitted, it is assumed to be the current scope.\n\
4616 If the first REGEXP is omitted, then all methods matching the second REGEXP\n\
4617 are listed.");
4618 #endif
4619 add_info ("sources", sources_info,
4620 "Source files in the program.");
4621
4622 add_com ("rbreak", class_breakpoint, rbreak_command,
4623 "Set a breakpoint for all functions matching REGEXP.");
4624
4625 if (xdb_commands)
4626 {
4627 add_com ("lf", class_info, sources_info, "Source files in the program");
4628 add_com ("lg", class_info, variables_info,
4629 "All global and static variable names, or those matching REGEXP.");
4630 }
4631
4632 /* Initialize the one built-in type that isn't language dependent... */
4633 builtin_type_error = init_type (TYPE_CODE_ERROR, 0, 0,
4634 "<unknown type>", (struct objfile *) NULL);
4635 }