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