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