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