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