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