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