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