Fri Aug 25 12:11:21 2000 David Taylor <taylor@texas.cygnus.com>
[deliverable/binutils-gdb.git] / gdb / symtab.c
CommitLineData
c906108c
SS
1/* Symbol table lookup for the GNU debugger, GDB.
2 Copyright 1986, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 1998
c5aa993b 3 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b
JM
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
c906108c
SS
21
22#include "defs.h"
23#include "symtab.h"
24#include "gdbtypes.h"
25#include "gdbcore.h"
26#include "frame.h"
27#include "target.h"
28#include "value.h"
29#include "symfile.h"
30#include "objfiles.h"
31#include "gdbcmd.h"
32#include "call-cmds.h"
88987551 33#include "gdb_regex.h"
c906108c
SS
34#include "expression.h"
35#include "language.h"
36#include "demangle.h"
37#include "inferior.h"
38
39#include "obstack.h"
40
41#include <sys/types.h>
42#include <fcntl.h>
43#include "gdb_string.h"
44#include "gdb_stat.h"
45#include <ctype.h>
46
47/* Prototype for one function in parser-defs.h,
48 instead of including that entire file. */
49
a14ed312 50extern char *find_template_name_end (char *);
c906108c
SS
51
52/* Prototypes for local functions */
53
a14ed312 54static int find_methods (struct type *, char *, struct symbol **);
c906108c 55
a14ed312 56static void completion_list_add_name (char *, char *, int, char *, char *);
c906108c 57
a14ed312
KB
58static void build_canonical_line_spec (struct symtab_and_line *,
59 char *, char ***);
c906108c 60
a14ed312
KB
61static struct symtabs_and_lines decode_line_2 (struct symbol *[],
62 int, int, char ***);
c906108c 63
a14ed312 64static void rbreak_command (char *, int);
c906108c 65
a14ed312 66static void types_info (char *, int);
c906108c 67
a14ed312 68static void functions_info (char *, int);
c906108c 69
a14ed312 70static void variables_info (char *, int);
c906108c 71
a14ed312 72static void sources_info (char *, int);
c906108c 73
a14ed312 74static void output_source_filename (char *, int *);
c906108c 75
a14ed312 76char *operator_chars (char *, char **);
c906108c 77
a14ed312 78static int find_line_common (struct linetable *, int, int *);
c906108c 79
b37bcaa8
KB
80static struct partial_symbol *lookup_partial_symbol (struct partial_symtab *,
81 const char *, int,
82 namespace_enum);
c906108c 83
a14ed312 84static struct symtab *lookup_symtab_1 (char *);
c906108c 85
a14ed312 86static void cplusplus_hint (char *);
c906108c 87
a14ed312 88static struct symbol *find_active_alias (struct symbol *sym, CORE_ADDR addr);
c906108c
SS
89
90/* This flag is used in hppa-tdep.c, and set in hp-symtab-read.c */
91/* Signals the presence of objects compiled by HP compilers */
92int hp_som_som_object_present = 0;
93
a14ed312 94static void fixup_section (struct general_symbol_info *, struct objfile *);
c906108c 95
a14ed312 96static int file_matches (char *, char **, int);
c906108c 97
a14ed312
KB
98static void print_symbol_info (namespace_enum,
99 struct symtab *, struct symbol *, int, char *);
c906108c 100
a14ed312 101static void print_msymbol_info (struct minimal_symbol *);
c906108c 102
a14ed312 103static void symtab_symbol_info (char *, namespace_enum, int);
c906108c 104
a14ed312 105static void overload_list_add_symbol (struct symbol *sym, char *oload_name);
392a587b 106
a14ed312 107void _initialize_symtab (void);
c906108c
SS
108
109/* */
110
111/* The single non-language-specific builtin type */
112struct type *builtin_type_error;
113
114/* Block in which the most recently searched-for symbol was found.
115 Might be better to make this a parameter to lookup_symbol and
116 value_of_this. */
117
118const struct block *block_found;
119
120char no_symtab_msg[] = "No symbol table is loaded. Use the \"file\" command.";
121
122/* While the C++ support is still in flux, issue a possibly helpful hint on
123 using the new command completion feature on single quoted demangled C++
124 symbols. Remove when loose ends are cleaned up. FIXME -fnf */
125
126static void
fba45db2 127cplusplus_hint (char *name)
c906108c
SS
128{
129 while (*name == '\'')
130 name++;
131 printf_filtered ("Hint: try '%s<TAB> or '%s<ESC-?>\n", name, name);
132 printf_filtered ("(Note leading single quote.)\n");
133}
134
135/* Check for a symtab of a specific name; first in symtabs, then in
136 psymtabs. *If* there is no '/' in the name, a match after a '/'
137 in the symtab filename will also work. */
138
139static struct symtab *
fba45db2 140lookup_symtab_1 (char *name)
c906108c
SS
141{
142 register struct symtab *s;
143 register struct partial_symtab *ps;
144 register char *slash;
145 register struct objfile *objfile;
146
c5aa993b 147got_symtab:
c906108c
SS
148
149 /* First, search for an exact match */
150
151 ALL_SYMTABS (objfile, s)
152 if (STREQ (name, s->filename))
c5aa993b 153 return s;
c906108c
SS
154
155 slash = strchr (name, '/');
156
157 /* Now, search for a matching tail (only if name doesn't have any dirs) */
158
159 if (!slash)
160 ALL_SYMTABS (objfile, s)
c5aa993b
JM
161 {
162 char *p = s->filename;
163 char *tail = strrchr (p, '/');
c906108c 164
c5aa993b
JM
165 if (tail)
166 p = tail + 1;
c906108c 167
c5aa993b
JM
168 if (STREQ (p, name))
169 return s;
170 }
c906108c
SS
171
172 /* Same search rules as above apply here, but now we look thru the
173 psymtabs. */
174
175 ps = lookup_partial_symtab (name);
176 if (!ps)
177 return (NULL);
178
c5aa993b 179 if (ps->readin)
c906108c 180 error ("Internal: readin %s pst for `%s' found when no symtab found.",
c5aa993b 181 ps->filename, name);
c906108c
SS
182
183 s = PSYMTAB_TO_SYMTAB (ps);
184
185 if (s)
186 return s;
187
188 /* At this point, we have located the psymtab for this file, but
189 the conversion to a symtab has failed. This usually happens
190 when we are looking up an include file. In this case,
191 PSYMTAB_TO_SYMTAB doesn't return a symtab, even though one has
192 been created. So, we need to run through the symtabs again in
193 order to find the file.
194 XXX - This is a crock, and should be fixed inside of the the
195 symbol parsing routines. */
196 goto got_symtab;
197}
198
199/* Lookup the symbol table of a source file named NAME. Try a couple
200 of variations if the first lookup doesn't work. */
201
202struct symtab *
fba45db2 203lookup_symtab (char *name)
c906108c
SS
204{
205 register struct symtab *s;
206#if 0
207 register char *copy;
208#endif
209
210 s = lookup_symtab_1 (name);
c5aa993b
JM
211 if (s)
212 return s;
c906108c
SS
213
214#if 0
215 /* This screws c-exp.y:yylex if there is both a type "tree" and a symtab
216 "tree.c". */
217
218 /* If name not found as specified, see if adding ".c" helps. */
219 /* Why is this? Is it just a user convenience? (If so, it's pretty
220 questionable in the presence of C++, FORTRAN, etc.). It's not in
221 the GDB manual. */
222
223 copy = (char *) alloca (strlen (name) + 3);
224 strcpy (copy, name);
225 strcat (copy, ".c");
226 s = lookup_symtab_1 (copy);
c5aa993b
JM
227 if (s)
228 return s;
c906108c
SS
229#endif /* 0 */
230
231 /* We didn't find anything; die. */
232 return 0;
233}
234
235/* Lookup the partial symbol table of a source file named NAME.
236 *If* there is no '/' in the name, a match after a '/'
237 in the psymtab filename will also work. */
238
239struct partial_symtab *
fba45db2 240lookup_partial_symtab (char *name)
c906108c
SS
241{
242 register struct partial_symtab *pst;
243 register struct objfile *objfile;
c5aa993b 244
c906108c 245 ALL_PSYMTABS (objfile, pst)
c5aa993b
JM
246 {
247 if (STREQ (name, pst->filename))
248 {
249 return (pst);
250 }
251 }
c906108c
SS
252
253 /* Now, search for a matching tail (only if name doesn't have any dirs) */
254
255 if (!strchr (name, '/'))
256 ALL_PSYMTABS (objfile, pst)
c5aa993b
JM
257 {
258 char *p = pst->filename;
259 char *tail = strrchr (p, '/');
c906108c 260
c5aa993b
JM
261 if (tail)
262 p = tail + 1;
c906108c 263
c5aa993b
JM
264 if (STREQ (p, name))
265 return (pst);
266 }
c906108c
SS
267
268 return (NULL);
269}
270\f
271/* Mangle a GDB method stub type. This actually reassembles the pieces of the
272 full method name, which consist of the class name (from T), the unadorned
273 method name from METHOD_ID, and the signature for the specific overload,
274 specified by SIGNATURE_ID. Note that this function is g++ specific. */
275
276char *
fba45db2 277gdb_mangle_name (struct type *type, int method_id, int signature_id)
c906108c
SS
278{
279 int mangled_name_len;
280 char *mangled_name;
281 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
282 struct fn_field *method = &f[signature_id];
283 char *field_name = TYPE_FN_FIELDLIST_NAME (type, method_id);
284 char *physname = TYPE_FN_FIELD_PHYSNAME (f, signature_id);
285 char *newname = type_name_no_tag (type);
286
287 /* Does the form of physname indicate that it is the full mangled name
288 of a constructor (not just the args)? */
289 int is_full_physname_constructor;
290
291 int is_constructor;
292 int is_destructor = DESTRUCTOR_PREFIX_P (physname);
293 /* Need a new type prefix. */
294 char *const_prefix = method->is_const ? "C" : "";
295 char *volatile_prefix = method->is_volatile ? "V" : "";
296 char buf[20];
297 int len = (newname == NULL ? 0 : strlen (newname));
298
c5aa993b
JM
299 is_full_physname_constructor =
300 ((physname[0] == '_' && physname[1] == '_' &&
301 (isdigit (physname[2]) || physname[2] == 'Q' || physname[2] == 't'))
302 || (strncmp (physname, "__ct", 4) == 0));
c906108c
SS
303
304 is_constructor =
c5aa993b 305 is_full_physname_constructor || (newname && STREQ (field_name, newname));
c906108c
SS
306
307 if (!is_destructor)
c5aa993b 308 is_destructor = (strncmp (physname, "__dt", 4) == 0);
c906108c
SS
309
310 if (is_destructor || is_full_physname_constructor)
311 {
c5aa993b
JM
312 mangled_name = (char *) xmalloc (strlen (physname) + 1);
313 strcpy (mangled_name, physname);
c906108c
SS
314 return mangled_name;
315 }
316
317 if (len == 0)
318 {
319 sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
320 }
321 else if (physname[0] == 't' || physname[0] == 'Q')
322 {
323 /* The physname for template and qualified methods already includes
c5aa993b 324 the class name. */
c906108c
SS
325 sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
326 newname = NULL;
327 len = 0;
328 }
329 else
330 {
331 sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len);
332 }
333 mangled_name_len = ((is_constructor ? 0 : strlen (field_name))
c5aa993b
JM
334 + strlen (buf) + len
335 + strlen (physname)
336 + 1);
c906108c
SS
337
338 /* Only needed for GNU-mangled names. ANSI-mangled names
339 work with the normal mechanisms. */
340 if (OPNAME_PREFIX_P (field_name))
341 {
342 const char *opname = cplus_mangle_opname (field_name + 3, 0);
343 if (opname == NULL)
344 error ("No mangling for \"%s\"", field_name);
345 mangled_name_len += strlen (opname);
c5aa993b 346 mangled_name = (char *) xmalloc (mangled_name_len);
c906108c
SS
347
348 strncpy (mangled_name, field_name, 3);
349 mangled_name[3] = '\0';
350 strcat (mangled_name, opname);
351 }
352 else
353 {
c5aa993b 354 mangled_name = (char *) xmalloc (mangled_name_len);
c906108c
SS
355 if (is_constructor)
356 mangled_name[0] = '\0';
357 else
358 strcpy (mangled_name, field_name);
359 }
360 strcat (mangled_name, buf);
361 /* If the class doesn't have a name, i.e. newname NULL, then we just
362 mangle it using 0 for the length of the class. Thus it gets mangled
c5aa993b 363 as something starting with `::' rather than `classname::'. */
c906108c
SS
364 if (newname != NULL)
365 strcat (mangled_name, newname);
366
367 strcat (mangled_name, physname);
368 return (mangled_name);
369}
c906108c
SS
370\f
371
c5aa993b 372
c906108c
SS
373/* Find which partial symtab on contains PC and SECTION. Return 0 if none. */
374
375struct partial_symtab *
fba45db2 376find_pc_sect_psymtab (CORE_ADDR pc, asection *section)
c906108c
SS
377{
378 register struct partial_symtab *pst;
379 register struct objfile *objfile;
380
381 ALL_PSYMTABS (objfile, pst)
c5aa993b 382 {
c5aa993b 383 if (pc >= pst->textlow && pc < pst->texthigh)
c5aa993b
JM
384 {
385 struct minimal_symbol *msymbol;
386 struct partial_symtab *tpst;
387
388 /* An objfile that has its functions reordered might have
389 many partial symbol tables containing the PC, but
390 we want the partial symbol table that contains the
391 function containing the PC. */
392 if (!(objfile->flags & OBJF_REORDERED) &&
393 section == 0) /* can't validate section this way */
394 return (pst);
395
396 msymbol = lookup_minimal_symbol_by_pc_section (pc, section);
397 if (msymbol == NULL)
398 return (pst);
399
400 for (tpst = pst; tpst != NULL; tpst = tpst->next)
401 {
c5aa993b 402 if (pc >= tpst->textlow && pc < tpst->texthigh)
c5aa993b
JM
403 {
404 struct partial_symbol *p;
c906108c 405
c5aa993b
JM
406 p = find_pc_sect_psymbol (tpst, pc, section);
407 if (p != NULL
408 && SYMBOL_VALUE_ADDRESS (p)
409 == SYMBOL_VALUE_ADDRESS (msymbol))
410 return (tpst);
411 }
412 }
413 return (pst);
414 }
415 }
c906108c
SS
416 return (NULL);
417}
418
419/* Find which partial symtab contains PC. Return 0 if none.
420 Backward compatibility, no section */
421
422struct partial_symtab *
fba45db2 423find_pc_psymtab (CORE_ADDR pc)
c906108c
SS
424{
425 return find_pc_sect_psymtab (pc, find_pc_mapped_section (pc));
426}
427
428/* Find which partial symbol within a psymtab matches PC and SECTION.
429 Return 0 if none. Check all psymtabs if PSYMTAB is 0. */
430
431struct partial_symbol *
fba45db2
KB
432find_pc_sect_psymbol (struct partial_symtab *psymtab, CORE_ADDR pc,
433 asection *section)
c906108c
SS
434{
435 struct partial_symbol *best = NULL, *p, **pp;
436 CORE_ADDR best_pc;
c5aa993b 437
c906108c
SS
438 if (!psymtab)
439 psymtab = find_pc_sect_psymtab (pc, section);
440 if (!psymtab)
441 return 0;
442
443 /* Cope with programs that start at address 0 */
444 best_pc = (psymtab->textlow != 0) ? psymtab->textlow - 1 : 0;
445
446 /* Search the global symbols as well as the static symbols, so that
447 find_pc_partial_function doesn't use a minimal symbol and thus
448 cache a bad endaddr. */
449 for (pp = psymtab->objfile->global_psymbols.list + psymtab->globals_offset;
c5aa993b
JM
450 (pp - (psymtab->objfile->global_psymbols.list + psymtab->globals_offset)
451 < psymtab->n_global_syms);
c906108c
SS
452 pp++)
453 {
454 p = *pp;
455 if (SYMBOL_NAMESPACE (p) == VAR_NAMESPACE
456 && SYMBOL_CLASS (p) == LOC_BLOCK
457 && pc >= SYMBOL_VALUE_ADDRESS (p)
458 && (SYMBOL_VALUE_ADDRESS (p) > best_pc
459 || (psymtab->textlow == 0
460 && best_pc == 0 && SYMBOL_VALUE_ADDRESS (p) == 0)))
461 {
c5aa993b 462 if (section) /* match on a specific section */
c906108c
SS
463 {
464 fixup_psymbol_section (p, psymtab->objfile);
465 if (SYMBOL_BFD_SECTION (p) != section)
466 continue;
467 }
468 best_pc = SYMBOL_VALUE_ADDRESS (p);
469 best = p;
470 }
471 }
472
473 for (pp = psymtab->objfile->static_psymbols.list + psymtab->statics_offset;
c5aa993b
JM
474 (pp - (psymtab->objfile->static_psymbols.list + psymtab->statics_offset)
475 < psymtab->n_static_syms);
c906108c
SS
476 pp++)
477 {
478 p = *pp;
479 if (SYMBOL_NAMESPACE (p) == VAR_NAMESPACE
480 && SYMBOL_CLASS (p) == LOC_BLOCK
481 && pc >= SYMBOL_VALUE_ADDRESS (p)
482 && (SYMBOL_VALUE_ADDRESS (p) > best_pc
c5aa993b 483 || (psymtab->textlow == 0
c906108c
SS
484 && best_pc == 0 && SYMBOL_VALUE_ADDRESS (p) == 0)))
485 {
c5aa993b 486 if (section) /* match on a specific section */
c906108c
SS
487 {
488 fixup_psymbol_section (p, psymtab->objfile);
489 if (SYMBOL_BFD_SECTION (p) != section)
490 continue;
491 }
492 best_pc = SYMBOL_VALUE_ADDRESS (p);
493 best = p;
494 }
495 }
496
497 return best;
498}
499
500/* Find which partial symbol within a psymtab matches PC. Return 0 if none.
501 Check all psymtabs if PSYMTAB is 0. Backwards compatibility, no section. */
502
503struct partial_symbol *
fba45db2 504find_pc_psymbol (struct partial_symtab *psymtab, CORE_ADDR pc)
c906108c
SS
505{
506 return find_pc_sect_psymbol (psymtab, pc, find_pc_mapped_section (pc));
507}
508\f
509/* Debug symbols usually don't have section information. We need to dig that
510 out of the minimal symbols and stash that in the debug symbol. */
511
512static void
fba45db2 513fixup_section (struct general_symbol_info *ginfo, struct objfile *objfile)
c906108c
SS
514{
515 struct minimal_symbol *msym;
516 msym = lookup_minimal_symbol (ginfo->name, NULL, objfile);
517
518 if (msym)
7a78d0ee
KB
519 {
520 ginfo->bfd_section = SYMBOL_BFD_SECTION (msym);
521 ginfo->section = SYMBOL_SECTION (msym);
522 }
c906108c
SS
523}
524
525struct symbol *
fba45db2 526fixup_symbol_section (struct symbol *sym, struct objfile *objfile)
c906108c
SS
527{
528 if (!sym)
529 return NULL;
530
531 if (SYMBOL_BFD_SECTION (sym))
532 return sym;
533
534 fixup_section (&sym->ginfo, objfile);
535
536 return sym;
537}
538
7a78d0ee 539struct partial_symbol *
fba45db2 540fixup_psymbol_section (struct partial_symbol *psym, struct objfile *objfile)
c906108c
SS
541{
542 if (!psym)
543 return NULL;
544
545 if (SYMBOL_BFD_SECTION (psym))
546 return psym;
547
548 fixup_section (&psym->ginfo, objfile);
549
550 return psym;
551}
552
553/* Find the definition for a specified symbol name NAME
554 in namespace NAMESPACE, visible from lexical block BLOCK.
555 Returns the struct symbol pointer, or zero if no symbol is found.
556 If SYMTAB is non-NULL, store the symbol table in which the
557 symbol was found there, or NULL if not found.
558 C++: if IS_A_FIELD_OF_THIS is nonzero on entry, check to see if
559 NAME is a field of the current implied argument `this'. If so set
560 *IS_A_FIELD_OF_THIS to 1, otherwise set it to zero.
561 BLOCK_FOUND is set to the block in which NAME is found (in the case of
562 a field of `this', value_of_this sets BLOCK_FOUND to the proper value.) */
563
564/* This function has a bunch of loops in it and it would seem to be
565 attractive to put in some QUIT's (though I'm not really sure
566 whether it can run long enough to be really important). But there
567 are a few calls for which it would appear to be bad news to quit
568 out of here: find_proc_desc in alpha-tdep.c and mips-tdep.c, and
569 nindy_frame_chain_valid in nindy-tdep.c. (Note that there is C++
570 code below which can error(), but that probably doesn't affect
571 these calls since they are looking for a known variable and thus
572 can probably assume it will never hit the C++ code). */
573
574struct symbol *
fba45db2
KB
575lookup_symbol (const char *name, register const struct block *block,
576 const namespace_enum namespace, int *is_a_field_of_this,
577 struct symtab **symtab)
c906108c
SS
578{
579 register struct symbol *sym;
580 register struct symtab *s = NULL;
581 register struct partial_symtab *ps;
582 struct blockvector *bv;
583 register struct objfile *objfile = NULL;
584 register struct block *b;
585 register struct minimal_symbol *msymbol;
586
63872f9d
JG
587 if (case_sensitivity == case_sensitive_off)
588 {
589 char *copy;
590 int len, i;
591
592 len = strlen (name);
593 copy = (char *) alloca (len + 1);
594 for (i= 0; i < len; i++)
595 copy[i] = tolower (name[i]);
596 copy[len] = 0;
597 name = copy;
598 }
599
c906108c
SS
600 /* Search specified block and its superiors. */
601
602 while (block != 0)
603 {
604 sym = lookup_block_symbol (block, name, namespace);
c5aa993b 605 if (sym)
c906108c
SS
606 {
607 block_found = block;
608 if (symtab != NULL)
609 {
610 /* Search the list of symtabs for one which contains the
c5aa993b 611 address of the start of this block. */
c906108c 612 ALL_SYMTABS (objfile, s)
c5aa993b
JM
613 {
614 bv = BLOCKVECTOR (s);
615 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
616 if (BLOCK_START (b) <= BLOCK_START (block)
617 && BLOCK_END (b) > BLOCK_START (block))
618 goto found;
619 }
620 found:
c906108c
SS
621 *symtab = s;
622 }
623
624 return fixup_symbol_section (sym, objfile);
625 }
626 block = BLOCK_SUPERBLOCK (block);
627 }
628
629 /* FIXME: this code is never executed--block is always NULL at this
630 point. What is it trying to do, anyway? We already should have
631 checked the STATIC_BLOCK above (it is the superblock of top-level
632 blocks). Why is VAR_NAMESPACE special-cased? */
633 /* Don't need to mess with the psymtabs; if we have a block,
634 that file is read in. If we don't, then we deal later with
635 all the psymtab stuff that needs checking. */
636 /* Note (RT): The following never-executed code looks unnecessary to me also.
637 * If we change the code to use the original (passed-in)
638 * value of 'block', we could cause it to execute, but then what
639 * would it do? The STATIC_BLOCK of the symtab containing the passed-in
640 * 'block' was already searched by the above code. And the STATIC_BLOCK's
641 * of *other* symtabs (those files not containing 'block' lexically)
642 * should not contain 'block' address-wise. So we wouldn't expect this
643 * code to find any 'sym''s that were not found above. I vote for
644 * deleting the following paragraph of code.
645 */
646 if (namespace == VAR_NAMESPACE && block != NULL)
647 {
648 struct block *b;
649 /* Find the right symtab. */
650 ALL_SYMTABS (objfile, s)
c5aa993b
JM
651 {
652 bv = BLOCKVECTOR (s);
653 b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
654 if (BLOCK_START (b) <= BLOCK_START (block)
655 && BLOCK_END (b) > BLOCK_START (block))
656 {
657 sym = lookup_block_symbol (b, name, VAR_NAMESPACE);
658 if (sym)
659 {
660 block_found = b;
661 if (symtab != NULL)
662 *symtab = s;
663 return fixup_symbol_section (sym, objfile);
664 }
665 }
666 }
c906108c
SS
667 }
668
669
670 /* C++: If requested to do so by the caller,
671 check to see if NAME is a field of `this'. */
672 if (is_a_field_of_this)
673 {
674 struct value *v = value_of_this (0);
c5aa993b 675
c906108c
SS
676 *is_a_field_of_this = 0;
677 if (v && check_field (v, name))
678 {
679 *is_a_field_of_this = 1;
680 if (symtab != NULL)
681 *symtab = NULL;
682 return NULL;
683 }
684 }
685
686 /* Now search all global blocks. Do the symtab's first, then
687 check the psymtab's. If a psymtab indicates the existence
688 of the desired name as a global, then do psymtab-to-symtab
689 conversion on the fly and return the found symbol. */
c5aa993b 690
c906108c 691 ALL_SYMTABS (objfile, s)
c5aa993b
JM
692 {
693 bv = BLOCKVECTOR (s);
694 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
695 sym = lookup_block_symbol (block, name, namespace);
696 if (sym)
697 {
698 block_found = block;
699 if (symtab != NULL)
700 *symtab = s;
701 return fixup_symbol_section (sym, objfile);
702 }
703 }
c906108c
SS
704
705#ifndef HPUXHPPA
706
707 /* Check for the possibility of the symbol being a function or
708 a mangled variable that is stored in one of the minimal symbol tables.
709 Eventually, all global symbols might be resolved in this way. */
c5aa993b 710
c906108c
SS
711 if (namespace == VAR_NAMESPACE)
712 {
713 msymbol = lookup_minimal_symbol (name, NULL, NULL);
714 if (msymbol != NULL)
715 {
716 s = find_pc_sect_symtab (SYMBOL_VALUE_ADDRESS (msymbol),
c5aa993b 717 SYMBOL_BFD_SECTION (msymbol));
c906108c
SS
718 if (s != NULL)
719 {
720 /* This is a function which has a symtab for its address. */
721 bv = BLOCKVECTOR (s);
722 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
723 sym = lookup_block_symbol (block, SYMBOL_NAME (msymbol),
724 namespace);
c5aa993b
JM
725 /* We kept static functions in minimal symbol table as well as
726 in static scope. We want to find them in the symbol table. */
727 if (!sym)
728 {
c906108c
SS
729 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
730 sym = lookup_block_symbol (block, SYMBOL_NAME (msymbol),
731 namespace);
732 }
733
734 /* sym == 0 if symbol was found in the minimal symbol table
c5aa993b
JM
735 but not in the symtab.
736 Return 0 to use the msymbol definition of "foo_".
c906108c 737
c5aa993b
JM
738 This happens for Fortran "foo_" symbols,
739 which are "foo" in the symtab.
c906108c 740
c5aa993b
JM
741 This can also happen if "asm" is used to make a
742 regular symbol but not a debugging symbol, e.g.
743 asm(".globl _main");
744 asm("_main:");
745 */
c906108c
SS
746
747 if (symtab != NULL)
748 *symtab = s;
749 return fixup_symbol_section (sym, objfile);
750 }
751 else if (MSYMBOL_TYPE (msymbol) != mst_text
752 && MSYMBOL_TYPE (msymbol) != mst_file_text
753 && !STREQ (name, SYMBOL_NAME (msymbol)))
754 {
755 /* This is a mangled variable, look it up by its
c5aa993b
JM
756 mangled name. */
757 return lookup_symbol (SYMBOL_NAME (msymbol), block,
c906108c
SS
758 namespace, is_a_field_of_this, symtab);
759 }
760 /* There are no debug symbols for this file, or we are looking
761 for an unmangled variable.
762 Try to find a matching static symbol below. */
763 }
764 }
c5aa993b 765
c906108c
SS
766#endif
767
768 ALL_PSYMTABS (objfile, ps)
c5aa993b
JM
769 {
770 if (!ps->readin && lookup_partial_symbol (ps, name, 1, namespace))
771 {
772 s = PSYMTAB_TO_SYMTAB (ps);
773 bv = BLOCKVECTOR (s);
774 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
775 sym = lookup_block_symbol (block, name, namespace);
776 if (!sym)
777 {
778 /* This shouldn't be necessary, but as a last resort
779 * try looking in the statics even though the psymtab
780 * claimed the symbol was global. It's possible that
781 * the psymtab gets it wrong in some cases.
782 */
783 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
784 sym = lookup_block_symbol (block, name, namespace);
785 if (!sym)
786 error ("Internal: global symbol `%s' found in %s psymtab but not in symtab.\n\
c906108c
SS
787%s may be an inlined function, or may be a template function\n\
788(if a template, try specifying an instantiation: %s<type>).",
c5aa993b
JM
789 name, ps->filename, name, name);
790 }
791 if (symtab != NULL)
792 *symtab = s;
793 return fixup_symbol_section (sym, objfile);
794 }
795 }
c906108c
SS
796
797 /* Now search all static file-level symbols.
798 Not strictly correct, but more useful than an error.
799 Do the symtabs first, then check the psymtabs.
800 If a psymtab indicates the existence
801 of the desired name as a file-level static, then do psymtab-to-symtab
802 conversion on the fly and return the found symbol. */
803
804 ALL_SYMTABS (objfile, s)
c5aa993b
JM
805 {
806 bv = BLOCKVECTOR (s);
807 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
808 sym = lookup_block_symbol (block, name, namespace);
809 if (sym)
810 {
811 block_found = block;
812 if (symtab != NULL)
813 *symtab = s;
814 return fixup_symbol_section (sym, objfile);
815 }
816 }
c906108c
SS
817
818 ALL_PSYMTABS (objfile, ps)
c5aa993b
JM
819 {
820 if (!ps->readin && lookup_partial_symbol (ps, name, 0, namespace))
821 {
822 s = PSYMTAB_TO_SYMTAB (ps);
823 bv = BLOCKVECTOR (s);
824 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
825 sym = lookup_block_symbol (block, name, namespace);
826 if (!sym)
827 {
828 /* This shouldn't be necessary, but as a last resort
829 * try looking in the globals even though the psymtab
830 * claimed the symbol was static. It's possible that
831 * the psymtab gets it wrong in some cases.
832 */
833 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
834 sym = lookup_block_symbol (block, name, namespace);
835 if (!sym)
836 error ("Internal: static symbol `%s' found in %s psymtab but not in symtab.\n\
c906108c
SS
837%s may be an inlined function, or may be a template function\n\
838(if a template, try specifying an instantiation: %s<type>).",
c5aa993b
JM
839 name, ps->filename, name, name);
840 }
841 if (symtab != NULL)
842 *symtab = s;
843 return fixup_symbol_section (sym, objfile);
844 }
845 }
c906108c
SS
846
847#ifdef HPUXHPPA
848
849 /* Check for the possibility of the symbol being a function or
850 a global variable that is stored in one of the minimal symbol tables.
851 The "minimal symbol table" is built from linker-supplied info.
852
853 RT: I moved this check to last, after the complete search of
854 the global (p)symtab's and static (p)symtab's. For HP-generated
855 symbol tables, this check was causing a premature exit from
856 lookup_symbol with NULL return, and thus messing up symbol lookups
857 of things like "c::f". It seems to me a check of the minimal
858 symbol table ought to be a last resort in any case. I'm vaguely
859 worried about the comment below which talks about FORTRAN routines "foo_"
860 though... is it saying we need to do the "minsym" check before
861 the static check in this case?
862 */
c5aa993b 863
c906108c
SS
864 if (namespace == VAR_NAMESPACE)
865 {
866 msymbol = lookup_minimal_symbol (name, NULL, NULL);
867 if (msymbol != NULL)
868 {
c5aa993b
JM
869 /* OK, we found a minimal symbol in spite of not
870 * finding any symbol. There are various possible
871 * explanations for this. One possibility is the symbol
872 * exists in code not compiled -g. Another possibility
873 * is that the 'psymtab' isn't doing its job.
874 * A third possibility, related to #2, is that we were confused
875 * by name-mangling. For instance, maybe the psymtab isn't
876 * doing its job because it only know about demangled
877 * names, but we were given a mangled name...
878 */
879
880 /* We first use the address in the msymbol to try to
881 * locate the appropriate symtab. Note that find_pc_symtab()
882 * has a side-effect of doing psymtab-to-symtab expansion,
883 * for the found symtab.
884 */
c906108c
SS
885 s = find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol));
886 if (s != NULL)
887 {
888 bv = BLOCKVECTOR (s);
889 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
890 sym = lookup_block_symbol (block, SYMBOL_NAME (msymbol),
891 namespace);
c5aa993b
JM
892 /* We kept static functions in minimal symbol table as well as
893 in static scope. We want to find them in the symbol table. */
894 if (!sym)
895 {
c906108c
SS
896 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
897 sym = lookup_block_symbol (block, SYMBOL_NAME (msymbol),
898 namespace);
899 }
c5aa993b
JM
900 /* If we found one, return it */
901 if (sym)
902 {
903 if (symtab != NULL)
904 *symtab = s;
905 return sym;
906 }
c906108c
SS
907
908 /* If we get here with sym == 0, the symbol was
c5aa993b
JM
909 found in the minimal symbol table
910 but not in the symtab.
911 Fall through and return 0 to use the msymbol
912 definition of "foo_".
913 (Note that outer code generally follows up a call
914 to this routine with a call to lookup_minimal_symbol(),
915 so a 0 return means we'll just flow into that other routine).
916
917 This happens for Fortran "foo_" symbols,
918 which are "foo" in the symtab.
919
920 This can also happen if "asm" is used to make a
921 regular symbol but not a debugging symbol, e.g.
922 asm(".globl _main");
923 asm("_main:");
924 */
c906108c
SS
925 }
926
c5aa993b
JM
927 /* If the lookup-by-address fails, try repeating the
928 * entire lookup process with the symbol name from
929 * the msymbol (if different from the original symbol name).
930 */
c906108c
SS
931 else if (MSYMBOL_TYPE (msymbol) != mst_text
932 && MSYMBOL_TYPE (msymbol) != mst_file_text
933 && !STREQ (name, SYMBOL_NAME (msymbol)))
934 {
935 return lookup_symbol (SYMBOL_NAME (msymbol), block,
936 namespace, is_a_field_of_this, symtab);
937 }
938 }
939 }
940
941#endif
942
943 if (symtab != NULL)
944 *symtab = NULL;
945 return 0;
946}
357e46e7 947
c906108c
SS
948/* Look, in partial_symtab PST, for symbol NAME. Check the global
949 symbols if GLOBAL, the static symbols if not */
950
951static struct partial_symbol *
fba45db2
KB
952lookup_partial_symbol (struct partial_symtab *pst, const char *name, int global,
953 namespace_enum namespace)
c906108c 954{
357e46e7 955 struct partial_symbol *temp;
c906108c
SS
956 struct partial_symbol **start, **psym;
957 struct partial_symbol **top, **bottom, **center;
958 int length = (global ? pst->n_global_syms : pst->n_static_syms);
959 int do_linear_search = 1;
357e46e7 960
c906108c
SS
961 if (length == 0)
962 {
963 return (NULL);
964 }
c906108c
SS
965 start = (global ?
966 pst->objfile->global_psymbols.list + pst->globals_offset :
c5aa993b 967 pst->objfile->static_psymbols.list + pst->statics_offset);
357e46e7 968
c5aa993b 969 if (global) /* This means we can use a binary search. */
c906108c
SS
970 {
971 do_linear_search = 0;
972
973 /* Binary search. This search is guaranteed to end with center
974 pointing at the earliest partial symbol with the correct
c5aa993b
JM
975 name. At that point *all* partial symbols with that name
976 will be checked against the correct namespace. */
c906108c
SS
977
978 bottom = start;
979 top = start + length - 1;
980 while (top > bottom)
981 {
982 center = bottom + (top - bottom) / 2;
983 if (!(center < top))
984 abort ();
985 if (!do_linear_search
357e46e7 986 && (SYMBOL_LANGUAGE (*center) == language_java))
c906108c
SS
987 {
988 do_linear_search = 1;
989 }
990 if (STRCMP (SYMBOL_NAME (*center), name) >= 0)
991 {
992 top = center;
993 }
994 else
995 {
996 bottom = center + 1;
997 }
998 }
999 if (!(top == bottom))
1000 abort ();
357e46e7
DB
1001
1002 /* djb - 2000-06-03 - Use SYMBOL_MATCHES_NAME, not a strcmp, so
1003 we don't have to force a linear search on C++. Probably holds true
1004 for JAVA as well, no way to check.*/
1005 while (SYMBOL_MATCHES_NAME (*top,name))
c906108c
SS
1006 {
1007 if (SYMBOL_NAMESPACE (*top) == namespace)
1008 {
357e46e7 1009 return (*top);
c906108c 1010 }
c5aa993b 1011 top++;
c906108c
SS
1012 }
1013 }
1014
1015 /* Can't use a binary search or else we found during the binary search that
1016 we should also do a linear search. */
1017
1018 if (do_linear_search)
357e46e7 1019 {
c906108c
SS
1020 for (psym = start; psym < start + length; psym++)
1021 {
1022 if (namespace == SYMBOL_NAMESPACE (*psym))
1023 {
1024 if (SYMBOL_MATCHES_NAME (*psym, name))
1025 {
1026 return (*psym);
1027 }
1028 }
1029 }
1030 }
1031
1032 return (NULL);
1033}
1034
1035/* Look up a type named NAME in the struct_namespace. The type returned
1036 must not be opaque -- i.e., must have at least one field defined
1037
1038 This code was modelled on lookup_symbol -- the parts not relevant to looking
1039 up types were just left out. In particular it's assumed here that types
1040 are available in struct_namespace and only at file-static or global blocks. */
1041
1042
1043struct type *
fba45db2 1044lookup_transparent_type (const char *name)
c906108c
SS
1045{
1046 register struct symbol *sym;
1047 register struct symtab *s = NULL;
1048 register struct partial_symtab *ps;
1049 struct blockvector *bv;
1050 register struct objfile *objfile;
1051 register struct block *block;
c906108c
SS
1052
1053 /* Now search all the global symbols. Do the symtab's first, then
1054 check the psymtab's. If a psymtab indicates the existence
1055 of the desired name as a global, then do psymtab-to-symtab
1056 conversion on the fly and return the found symbol. */
c5aa993b 1057
c906108c 1058 ALL_SYMTABS (objfile, s)
c5aa993b
JM
1059 {
1060 bv = BLOCKVECTOR (s);
1061 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1062 sym = lookup_block_symbol (block, name, STRUCT_NAMESPACE);
1063 if (sym && !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1064 {
1065 return SYMBOL_TYPE (sym);
1066 }
1067 }
c906108c
SS
1068
1069 ALL_PSYMTABS (objfile, ps)
c5aa993b
JM
1070 {
1071 if (!ps->readin && lookup_partial_symbol (ps, name, 1, STRUCT_NAMESPACE))
1072 {
1073 s = PSYMTAB_TO_SYMTAB (ps);
1074 bv = BLOCKVECTOR (s);
1075 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1076 sym = lookup_block_symbol (block, name, STRUCT_NAMESPACE);
1077 if (!sym)
1078 {
1079 /* This shouldn't be necessary, but as a last resort
1080 * try looking in the statics even though the psymtab
1081 * claimed the symbol was global. It's possible that
1082 * the psymtab gets it wrong in some cases.
1083 */
1084 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1085 sym = lookup_block_symbol (block, name, STRUCT_NAMESPACE);
1086 if (!sym)
1087 error ("Internal: global symbol `%s' found in %s psymtab but not in symtab.\n\
c906108c
SS
1088%s may be an inlined function, or may be a template function\n\
1089(if a template, try specifying an instantiation: %s<type>).",
c5aa993b
JM
1090 name, ps->filename, name, name);
1091 }
1092 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1093 return SYMBOL_TYPE (sym);
1094 }
1095 }
c906108c
SS
1096
1097 /* Now search the static file-level symbols.
1098 Not strictly correct, but more useful than an error.
1099 Do the symtab's first, then
1100 check the psymtab's. If a psymtab indicates the existence
1101 of the desired name as a file-level static, then do psymtab-to-symtab
1102 conversion on the fly and return the found symbol.
1103 */
1104
1105 ALL_SYMTABS (objfile, s)
c5aa993b
JM
1106 {
1107 bv = BLOCKVECTOR (s);
1108 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1109 sym = lookup_block_symbol (block, name, STRUCT_NAMESPACE);
1110 if (sym && !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1111 {
1112 return SYMBOL_TYPE (sym);
1113 }
1114 }
c906108c
SS
1115
1116 ALL_PSYMTABS (objfile, ps)
c5aa993b
JM
1117 {
1118 if (!ps->readin && lookup_partial_symbol (ps, name, 0, STRUCT_NAMESPACE))
1119 {
1120 s = PSYMTAB_TO_SYMTAB (ps);
1121 bv = BLOCKVECTOR (s);
1122 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1123 sym = lookup_block_symbol (block, name, STRUCT_NAMESPACE);
1124 if (!sym)
1125 {
1126 /* This shouldn't be necessary, but as a last resort
1127 * try looking in the globals even though the psymtab
1128 * claimed the symbol was static. It's possible that
1129 * the psymtab gets it wrong in some cases.
1130 */
1131 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1132 sym = lookup_block_symbol (block, name, STRUCT_NAMESPACE);
1133 if (!sym)
1134 error ("Internal: static symbol `%s' found in %s psymtab but not in symtab.\n\
c906108c
SS
1135%s may be an inlined function, or may be a template function\n\
1136(if a template, try specifying an instantiation: %s<type>).",
c5aa993b
JM
1137 name, ps->filename, name, name);
1138 }
1139 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1140 return SYMBOL_TYPE (sym);
1141 }
1142 }
c906108c
SS
1143 return (struct type *) 0;
1144}
1145
1146
1147/* Find the psymtab containing main(). */
1148/* FIXME: What about languages without main() or specially linked
1149 executables that have no main() ? */
1150
1151struct partial_symtab *
fba45db2 1152find_main_psymtab (void)
c906108c
SS
1153{
1154 register struct partial_symtab *pst;
1155 register struct objfile *objfile;
1156
1157 ALL_PSYMTABS (objfile, pst)
c5aa993b
JM
1158 {
1159 if (lookup_partial_symbol (pst, "main", 1, VAR_NAMESPACE))
1160 {
1161 return (pst);
1162 }
1163 }
c906108c
SS
1164 return (NULL);
1165}
1166
1167/* Search BLOCK for symbol NAME in NAMESPACE.
1168
1169 Note that if NAME is the demangled form of a C++ symbol, we will fail
1170 to find a match during the binary search of the non-encoded names, but
1171 for now we don't worry about the slight inefficiency of looking for
1172 a match we'll never find, since it will go pretty quick. Once the
1173 binary search terminates, we drop through and do a straight linear
1174 search on the symbols. Each symbol which is marked as being a C++
1175 symbol (language_cplus set) has both the encoded and non-encoded names
1176 tested for a match. */
1177
1178struct symbol *
fba45db2
KB
1179lookup_block_symbol (register const struct block *block, const char *name,
1180 const namespace_enum namespace)
c906108c
SS
1181{
1182 register int bot, top, inc;
1183 register struct symbol *sym;
1184 register struct symbol *sym_found = NULL;
1185 register int do_linear_search = 1;
1186
1187 /* If the blocks's symbols were sorted, start with a binary search. */
1188
1189 if (BLOCK_SHOULD_SORT (block))
1190 {
1191 /* Reset the linear search flag so if the binary search fails, we
c5aa993b
JM
1192 won't do the linear search once unless we find some reason to
1193 do so, such as finding a C++ symbol during the binary search.
1194 Note that for C++ modules, ALL the symbols in a block should
1195 end up marked as C++ symbols. */
c906108c
SS
1196
1197 do_linear_search = 0;
1198 top = BLOCK_NSYMS (block);
1199 bot = 0;
1200
1201 /* Advance BOT to not far before the first symbol whose name is NAME. */
1202
1203 while (1)
1204 {
1205 inc = (top - bot + 1);
1206 /* No need to keep binary searching for the last few bits worth. */
1207 if (inc < 4)
1208 {
1209 break;
1210 }
1211 inc = (inc >> 1) + bot;
1212 sym = BLOCK_SYM (block, inc);
1213 if (!do_linear_search
1214 && (SYMBOL_LANGUAGE (sym) == language_cplus
1215 || SYMBOL_LANGUAGE (sym) == language_java
c5aa993b 1216 ))
c906108c
SS
1217 {
1218 do_linear_search = 1;
1219 }
1220 if (SYMBOL_NAME (sym)[0] < name[0])
1221 {
1222 bot = inc;
1223 }
1224 else if (SYMBOL_NAME (sym)[0] > name[0])
1225 {
1226 top = inc;
1227 }
1228 else if (STRCMP (SYMBOL_NAME (sym), name) < 0)
1229 {
1230 bot = inc;
1231 }
1232 else
1233 {
1234 top = inc;
1235 }
1236 }
1237
1238 /* Now scan forward until we run out of symbols, find one whose
c5aa993b
JM
1239 name is greater than NAME, or find one we want. If there is
1240 more than one symbol with the right name and namespace, we
1241 return the first one; I believe it is now impossible for us
1242 to encounter two symbols with the same name and namespace
1243 here, because blocks containing argument symbols are no
1244 longer sorted. */
c906108c
SS
1245
1246 top = BLOCK_NSYMS (block);
1247 while (bot < top)
1248 {
1249 sym = BLOCK_SYM (block, bot);
1250 inc = SYMBOL_NAME (sym)[0] - name[0];
1251 if (inc == 0)
1252 {
1253 inc = STRCMP (SYMBOL_NAME (sym), name);
1254 }
1255 if (inc == 0 && SYMBOL_NAMESPACE (sym) == namespace)
1256 {
1257 return (sym);
1258 }
1259 if (inc > 0)
1260 {
1261 break;
1262 }
1263 bot++;
1264 }
1265 }
1266
1267 /* Here if block isn't sorted, or we fail to find a match during the
1268 binary search above. If during the binary search above, we find a
1269 symbol which is a C++ symbol, then we have re-enabled the linear
1270 search flag which was reset when starting the binary search.
1271
1272 This loop is equivalent to the loop above, but hacked greatly for speed.
1273
1274 Note that parameter symbols do not always show up last in the
1275 list; this loop makes sure to take anything else other than
1276 parameter symbols first; it only uses parameter symbols as a
1277 last resort. Note that this only takes up extra computation
1278 time on a match. */
1279
1280 if (do_linear_search)
1281 {
1282 top = BLOCK_NSYMS (block);
1283 bot = 0;
1284 while (bot < top)
1285 {
1286 sym = BLOCK_SYM (block, bot);
1287 if (SYMBOL_NAMESPACE (sym) == namespace &&
1288 SYMBOL_MATCHES_NAME (sym, name))
1289 {
1290 /* If SYM has aliases, then use any alias that is active
c5aa993b
JM
1291 at the current PC. If no alias is active at the current
1292 PC, then use the main symbol.
c906108c 1293
c5aa993b 1294 ?!? Is checking the current pc correct? Is this routine
a0b3c4fd
JM
1295 ever called to look up a symbol from another context?
1296
1297 FIXME: No, it's not correct. If someone sets a
1298 conditional breakpoint at an address, then the
1299 breakpoint's `struct expression' should refer to the
1300 `struct symbol' appropriate for the breakpoint's
1301 address, which may not be the PC.
1302
1303 Even if it were never called from another context,
1304 it's totally bizarre for lookup_symbol's behavior to
1305 depend on the value of the inferior's current PC. We
1306 should pass in the appropriate PC as well as the
1307 block. The interface to lookup_symbol should change
1308 to require the caller to provide a PC. */
1309
c5aa993b
JM
1310 if (SYMBOL_ALIASES (sym))
1311 sym = find_active_alias (sym, read_pc ());
c906108c
SS
1312
1313 sym_found = sym;
1314 if (SYMBOL_CLASS (sym) != LOC_ARG &&
1315 SYMBOL_CLASS (sym) != LOC_LOCAL_ARG &&
1316 SYMBOL_CLASS (sym) != LOC_REF_ARG &&
1317 SYMBOL_CLASS (sym) != LOC_REGPARM &&
1318 SYMBOL_CLASS (sym) != LOC_REGPARM_ADDR &&
1319 SYMBOL_CLASS (sym) != LOC_BASEREG_ARG)
1320 {
1321 break;
1322 }
1323 }
1324 bot++;
1325 }
1326 }
1327 return (sym_found); /* Will be NULL if not found. */
1328}
1329
1330/* Given a main symbol SYM and ADDR, search through the alias
1331 list to determine if an alias is active at ADDR and return
1332 the active alias.
1333
1334 If no alias is active, then return SYM. */
1335
1336static struct symbol *
fba45db2 1337find_active_alias (struct symbol *sym, CORE_ADDR addr)
c906108c
SS
1338{
1339 struct range_list *r;
1340 struct alias_list *aliases;
1341
1342 /* If we have aliases, check them first. */
1343 aliases = SYMBOL_ALIASES (sym);
1344
1345 while (aliases)
1346 {
1347 if (!SYMBOL_RANGES (aliases->sym))
c5aa993b 1348 return aliases->sym;
c906108c
SS
1349 for (r = SYMBOL_RANGES (aliases->sym); r; r = r->next)
1350 {
1351 if (r->start <= addr && r->end > addr)
1352 return aliases->sym;
1353 }
1354 aliases = aliases->next;
1355 }
1356
1357 /* Nothing found, return the main symbol. */
1358 return sym;
1359}
c906108c 1360\f
c5aa993b 1361
c906108c
SS
1362/* Return the symbol for the function which contains a specified
1363 lexical block, described by a struct block BL. */
1364
1365struct symbol *
fba45db2 1366block_function (struct block *bl)
c906108c
SS
1367{
1368 while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0)
1369 bl = BLOCK_SUPERBLOCK (bl);
1370
1371 return BLOCK_FUNCTION (bl);
1372}
1373
1374/* Find the symtab associated with PC and SECTION. Look through the
1375 psymtabs and read in another symtab if necessary. */
1376
1377struct symtab *
fba45db2 1378find_pc_sect_symtab (CORE_ADDR pc, asection *section)
c906108c
SS
1379{
1380 register struct block *b;
1381 struct blockvector *bv;
1382 register struct symtab *s = NULL;
1383 register struct symtab *best_s = NULL;
1384 register struct partial_symtab *ps;
1385 register struct objfile *objfile;
1386 CORE_ADDR distance = 0;
1387
1388 /* Search all symtabs for the one whose file contains our address, and which
1389 is the smallest of all the ones containing the address. This is designed
1390 to deal with a case like symtab a is at 0x1000-0x2000 and 0x3000-0x4000
1391 and symtab b is at 0x2000-0x3000. So the GLOBAL_BLOCK for a is from
1392 0x1000-0x4000, but for address 0x2345 we want to return symtab b.
1393
1394 This happens for native ecoff format, where code from included files
1395 gets its own symtab. The symtab for the included file should have
1396 been read in already via the dependency mechanism.
1397 It might be swifter to create several symtabs with the same name
1398 like xcoff does (I'm not sure).
1399
1400 It also happens for objfiles that have their functions reordered.
1401 For these, the symtab we are looking for is not necessarily read in. */
1402
1403 ALL_SYMTABS (objfile, s)
c5aa993b
JM
1404 {
1405 bv = BLOCKVECTOR (s);
1406 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
c906108c 1407
c5aa993b 1408 if (BLOCK_START (b) <= pc
c5aa993b 1409 && BLOCK_END (b) > pc
c5aa993b
JM
1410 && (distance == 0
1411 || BLOCK_END (b) - BLOCK_START (b) < distance))
1412 {
1413 /* For an objfile that has its functions reordered,
1414 find_pc_psymtab will find the proper partial symbol table
1415 and we simply return its corresponding symtab. */
1416 /* In order to better support objfiles that contain both
1417 stabs and coff debugging info, we continue on if a psymtab
1418 can't be found. */
1419 if ((objfile->flags & OBJF_REORDERED) && objfile->psymtabs)
1420 {
1421 ps = find_pc_sect_psymtab (pc, section);
1422 if (ps)
1423 return PSYMTAB_TO_SYMTAB (ps);
1424 }
1425 if (section != 0)
1426 {
1427 int i;
c906108c 1428
c5aa993b
JM
1429 for (i = 0; i < b->nsyms; i++)
1430 {
1431 fixup_symbol_section (b->sym[i], objfile);
1432 if (section == SYMBOL_BFD_SECTION (b->sym[i]))
1433 break;
1434 }
1435 if (i >= b->nsyms)
1436 continue; /* no symbol in this symtab matches section */
1437 }
1438 distance = BLOCK_END (b) - BLOCK_START (b);
1439 best_s = s;
1440 }
1441 }
c906108c
SS
1442
1443 if (best_s != NULL)
c5aa993b 1444 return (best_s);
c906108c
SS
1445
1446 s = NULL;
1447 ps = find_pc_sect_psymtab (pc, section);
1448 if (ps)
1449 {
1450 if (ps->readin)
1451 /* Might want to error() here (in case symtab is corrupt and
1452 will cause a core dump), but maybe we can successfully
1453 continue, so let's not. */
c906108c 1454 warning ("\
d730266b
AC
1455(Internal error: pc 0x%s in read in psymtab, but not in symtab.)\n",
1456 paddr_nz (pc));
c906108c
SS
1457 s = PSYMTAB_TO_SYMTAB (ps);
1458 }
1459 return (s);
1460}
1461
1462/* Find the symtab associated with PC. Look through the psymtabs and
1463 read in another symtab if necessary. Backward compatibility, no section */
1464
1465struct symtab *
fba45db2 1466find_pc_symtab (CORE_ADDR pc)
c906108c
SS
1467{
1468 return find_pc_sect_symtab (pc, find_pc_mapped_section (pc));
1469}
c906108c 1470\f
c5aa993b 1471
c906108c
SS
1472#if 0
1473
1474/* Find the closest symbol value (of any sort -- function or variable)
1475 for a given address value. Slow but complete. (currently unused,
1476 mainly because it is too slow. We could fix it if each symtab and
1477 psymtab had contained in it the addresses ranges of each of its
1478 sections, which also would be required to make things like "info
1479 line *0x2345" cause psymtabs to be converted to symtabs). */
1480
1481struct symbol *
fba45db2 1482find_addr_symbol (CORE_ADDR addr, struct symtab **symtabp, CORE_ADDR *symaddrp)
c906108c
SS
1483{
1484 struct symtab *symtab, *best_symtab;
1485 struct objfile *objfile;
1486 register int bot, top;
1487 register struct symbol *sym;
1488 register CORE_ADDR sym_addr;
1489 struct block *block;
1490 int blocknum;
1491
1492 /* Info on best symbol seen so far */
1493
1494 register CORE_ADDR best_sym_addr = 0;
1495 struct symbol *best_sym = 0;
1496
1497 /* FIXME -- we should pull in all the psymtabs, too! */
1498 ALL_SYMTABS (objfile, symtab)
c5aa993b
JM
1499 {
1500 /* Search the global and static blocks in this symtab for
1501 the closest symbol-address to the desired address. */
c906108c 1502
c5aa993b
JM
1503 for (blocknum = GLOBAL_BLOCK; blocknum <= STATIC_BLOCK; blocknum++)
1504 {
1505 QUIT;
1506 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), blocknum);
1507 top = BLOCK_NSYMS (block);
1508 for (bot = 0; bot < top; bot++)
1509 {
1510 sym = BLOCK_SYM (block, bot);
1511 switch (SYMBOL_CLASS (sym))
1512 {
1513 case LOC_STATIC:
1514 case LOC_LABEL:
1515 sym_addr = SYMBOL_VALUE_ADDRESS (sym);
1516 break;
1517
1518 case LOC_INDIRECT:
1519 sym_addr = SYMBOL_VALUE_ADDRESS (sym);
1520 /* An indirect symbol really lives at *sym_addr,
1521 * so an indirection needs to be done.
1522 * However, I am leaving this commented out because it's
1523 * expensive, and it's possible that symbolization
1524 * could be done without an active process (in
1525 * case this read_memory will fail). RT
1526 sym_addr = read_memory_unsigned_integer
1527 (sym_addr, TARGET_PTR_BIT / TARGET_CHAR_BIT);
1528 */
1529 break;
c906108c 1530
c5aa993b
JM
1531 case LOC_BLOCK:
1532 sym_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
1533 break;
c906108c 1534
c5aa993b
JM
1535 default:
1536 continue;
1537 }
c906108c 1538
c5aa993b
JM
1539 if (sym_addr <= addr)
1540 if (sym_addr > best_sym_addr)
1541 {
1542 /* Quit if we found an exact match. */
1543 best_sym = sym;
1544 best_sym_addr = sym_addr;
1545 best_symtab = symtab;
1546 if (sym_addr == addr)
1547 goto done;
1548 }
1549 }
1550 }
1551 }
c906108c 1552
c5aa993b 1553done:
c906108c
SS
1554 if (symtabp)
1555 *symtabp = best_symtab;
1556 if (symaddrp)
1557 *symaddrp = best_sym_addr;
1558 return best_sym;
1559}
1560#endif /* 0 */
1561
1562/* Find the source file and line number for a given PC value and section.
1563 Return a structure containing a symtab pointer, a line number,
1564 and a pc range for the entire source line.
1565 The value's .pc field is NOT the specified pc.
1566 NOTCURRENT nonzero means, if specified pc is on a line boundary,
1567 use the line that ends there. Otherwise, in that case, the line
1568 that begins there is used. */
1569
1570/* The big complication here is that a line may start in one file, and end just
1571 before the start of another file. This usually occurs when you #include
1572 code in the middle of a subroutine. To properly find the end of a line's PC
1573 range, we must search all symtabs associated with this compilation unit, and
1574 find the one whose first PC is closer than that of the next line in this
1575 symtab. */
1576
1577/* If it's worth the effort, we could be using a binary search. */
1578
1579struct symtab_and_line
fba45db2 1580find_pc_sect_line (CORE_ADDR pc, struct sec *section, int notcurrent)
c906108c
SS
1581{
1582 struct symtab *s;
1583 register struct linetable *l;
1584 register int len;
1585 register int i;
1586 register struct linetable_entry *item;
1587 struct symtab_and_line val;
1588 struct blockvector *bv;
1589 struct minimal_symbol *msymbol;
1590 struct minimal_symbol *mfunsym;
1591
1592 /* Info on best line seen so far, and where it starts, and its file. */
1593
1594 struct linetable_entry *best = NULL;
1595 CORE_ADDR best_end = 0;
1596 struct symtab *best_symtab = 0;
1597
1598 /* Store here the first line number
1599 of a file which contains the line at the smallest pc after PC.
1600 If we don't find a line whose range contains PC,
1601 we will use a line one less than this,
1602 with a range from the start of that file to the first line's pc. */
1603 struct linetable_entry *alt = NULL;
1604 struct symtab *alt_symtab = 0;
1605
1606 /* Info on best line seen in this file. */
1607
1608 struct linetable_entry *prev;
1609
1610 /* If this pc is not from the current frame,
1611 it is the address of the end of a call instruction.
1612 Quite likely that is the start of the following statement.
1613 But what we want is the statement containing the instruction.
1614 Fudge the pc to make sure we get that. */
1615
c5aa993b 1616 INIT_SAL (&val); /* initialize to zeroes */
c906108c
SS
1617
1618 if (notcurrent)
1619 pc -= 1;
1620
c5aa993b 1621 /* elz: added this because this function returned the wrong
c906108c
SS
1622 information if the pc belongs to a stub (import/export)
1623 to call a shlib function. This stub would be anywhere between
1624 two functions in the target, and the line info was erroneously
1625 taken to be the one of the line before the pc.
c5aa993b 1626 */
c906108c 1627 /* RT: Further explanation:
c5aa993b 1628
c906108c
SS
1629 * We have stubs (trampolines) inserted between procedures.
1630 *
1631 * Example: "shr1" exists in a shared library, and a "shr1" stub also
1632 * exists in the main image.
1633 *
1634 * In the minimal symbol table, we have a bunch of symbols
1635 * sorted by start address. The stubs are marked as "trampoline",
1636 * the others appear as text. E.g.:
1637 *
1638 * Minimal symbol table for main image
1639 * main: code for main (text symbol)
1640 * shr1: stub (trampoline symbol)
1641 * foo: code for foo (text symbol)
1642 * ...
1643 * Minimal symbol table for "shr1" image:
1644 * ...
1645 * shr1: code for shr1 (text symbol)
1646 * ...
1647 *
1648 * So the code below is trying to detect if we are in the stub
1649 * ("shr1" stub), and if so, find the real code ("shr1" trampoline),
1650 * and if found, do the symbolization from the real-code address
1651 * rather than the stub address.
1652 *
1653 * Assumptions being made about the minimal symbol table:
1654 * 1. lookup_minimal_symbol_by_pc() will return a trampoline only
1655 * if we're really in the trampoline. If we're beyond it (say
1656 * we're in "foo" in the above example), it'll have a closer
1657 * symbol (the "foo" text symbol for example) and will not
1658 * return the trampoline.
1659 * 2. lookup_minimal_symbol_text() will find a real text symbol
1660 * corresponding to the trampoline, and whose address will
1661 * be different than the trampoline address. I put in a sanity
1662 * check for the address being the same, to avoid an
1663 * infinite recursion.
1664 */
c5aa993b
JM
1665 msymbol = lookup_minimal_symbol_by_pc (pc);
1666 if (msymbol != NULL)
c906108c 1667 if (MSYMBOL_TYPE (msymbol) == mst_solib_trampoline)
c5aa993b
JM
1668 {
1669 mfunsym = lookup_minimal_symbol_text (SYMBOL_NAME (msymbol), NULL, NULL);
1670 if (mfunsym == NULL)
1671 /* I eliminated this warning since it is coming out
1672 * in the following situation:
1673 * gdb shmain // test program with shared libraries
1674 * (gdb) break shr1 // function in shared lib
1675 * Warning: In stub for ...
1676 * In the above situation, the shared lib is not loaded yet,
1677 * so of course we can't find the real func/line info,
1678 * but the "break" still works, and the warning is annoying.
1679 * So I commented out the warning. RT */
1680 /* warning ("In stub for %s; unable to find real function/line info", SYMBOL_NAME(msymbol)) */ ;
1681 /* fall through */
1682 else if (SYMBOL_VALUE (mfunsym) == SYMBOL_VALUE (msymbol))
1683 /* Avoid infinite recursion */
1684 /* See above comment about why warning is commented out */
1685 /* warning ("In stub for %s; unable to find real function/line info", SYMBOL_NAME(msymbol)) */ ;
1686 /* fall through */
1687 else
1688 return find_pc_line (SYMBOL_VALUE (mfunsym), 0);
1689 }
c906108c
SS
1690
1691
1692 s = find_pc_sect_symtab (pc, section);
1693 if (!s)
1694 {
1695 /* if no symbol information, return previous pc */
1696 if (notcurrent)
1697 pc++;
1698 val.pc = pc;
1699 return val;
1700 }
1701
1702 bv = BLOCKVECTOR (s);
1703
1704 /* Look at all the symtabs that share this blockvector.
1705 They all have the same apriori range, that we found was right;
1706 but they have different line tables. */
1707
1708 for (; s && BLOCKVECTOR (s) == bv; s = s->next)
1709 {
1710 /* Find the best line in this symtab. */
1711 l = LINETABLE (s);
1712 if (!l)
c5aa993b 1713 continue;
c906108c
SS
1714 len = l->nitems;
1715 if (len <= 0)
1716 {
1717 /* I think len can be zero if the symtab lacks line numbers
1718 (e.g. gcc -g1). (Either that or the LINETABLE is NULL;
1719 I'm not sure which, and maybe it depends on the symbol
1720 reader). */
1721 continue;
1722 }
1723
1724 prev = NULL;
1725 item = l->item; /* Get first line info */
1726
1727 /* Is this file's first line closer than the first lines of other files?
c5aa993b 1728 If so, record this file, and its first line, as best alternate. */
c906108c
SS
1729 if (item->pc > pc && (!alt || item->pc < alt->pc))
1730 {
1731 alt = item;
1732 alt_symtab = s;
1733 }
1734
1735 for (i = 0; i < len; i++, item++)
1736 {
1737 /* Leave prev pointing to the linetable entry for the last line
1738 that started at or before PC. */
1739 if (item->pc > pc)
1740 break;
1741
1742 prev = item;
1743 }
1744
1745 /* At this point, prev points at the line whose start addr is <= pc, and
c5aa993b
JM
1746 item points at the next line. If we ran off the end of the linetable
1747 (pc >= start of the last line), then prev == item. If pc < start of
1748 the first line, prev will not be set. */
c906108c
SS
1749
1750 /* Is this file's best line closer than the best in the other files?
c5aa993b 1751 If so, record this file, and its best line, as best so far. */
c906108c
SS
1752
1753 if (prev && (!best || prev->pc > best->pc))
1754 {
1755 best = prev;
1756 best_symtab = s;
1757 /* If another line is in the linetable, and its PC is closer
1758 than the best_end we currently have, take it as best_end. */
1759 if (i < len && (best_end == 0 || best_end > item->pc))
1760 best_end = item->pc;
1761 }
1762 }
1763
1764 if (!best_symtab)
1765 {
1766 if (!alt_symtab)
1767 { /* If we didn't find any line # info, just
1768 return zeros. */
1769 val.pc = pc;
1770 }
1771 else
1772 {
1773 val.symtab = alt_symtab;
1774 val.line = alt->line - 1;
1775
1776 /* Don't return line 0, that means that we didn't find the line. */
c5aa993b
JM
1777 if (val.line == 0)
1778 ++val.line;
c906108c
SS
1779
1780 val.pc = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
1781 val.end = alt->pc;
1782 }
1783 }
1784 else
1785 {
1786 val.symtab = best_symtab;
1787 val.line = best->line;
1788 val.pc = best->pc;
1789 if (best_end && (!alt || best_end < alt->pc))
1790 val.end = best_end;
1791 else if (alt)
1792 val.end = alt->pc;
1793 else
1794 val.end = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
1795 }
1796 val.section = section;
1797 return val;
1798}
1799
1800/* Backward compatibility (no section) */
1801
1802struct symtab_and_line
fba45db2 1803find_pc_line (CORE_ADDR pc, int notcurrent)
c906108c 1804{
c5aa993b 1805 asection *section;
c906108c
SS
1806
1807 section = find_pc_overlay (pc);
1808 if (pc_in_unmapped_range (pc, section))
1809 pc = overlay_mapped_address (pc, section);
1810 return find_pc_sect_line (pc, section, notcurrent);
1811}
c906108c 1812\f
c5aa993b 1813
a14ed312 1814static struct symtab *find_line_symtab (struct symtab *, int, int *, int *);
c906108c
SS
1815
1816/* Find line number LINE in any symtab whose name is the same as
1817 SYMTAB.
1818
1819 If found, return the symtab that contains the linetable in which it was
1820 found, set *INDEX to the index in the linetable of the best entry
1821 found, and set *EXACT_MATCH nonzero if the value returned is an
1822 exact match.
1823
1824 If not found, return NULL. */
1825
c5aa993b 1826static struct symtab *
fba45db2 1827find_line_symtab (struct symtab *symtab, int line, int *index, int *exact_match)
c906108c
SS
1828{
1829 int exact;
1830
1831 /* BEST_INDEX and BEST_LINETABLE identify the smallest linenumber > LINE
1832 so far seen. */
1833
1834 int best_index;
1835 struct linetable *best_linetable;
1836 struct symtab *best_symtab;
1837
1838 /* First try looking it up in the given symtab. */
1839 best_linetable = LINETABLE (symtab);
1840 best_symtab = symtab;
1841 best_index = find_line_common (best_linetable, line, &exact);
1842 if (best_index < 0 || !exact)
1843 {
1844 /* Didn't find an exact match. So we better keep looking for
c5aa993b
JM
1845 another symtab with the same name. In the case of xcoff,
1846 multiple csects for one source file (produced by IBM's FORTRAN
1847 compiler) produce multiple symtabs (this is unavoidable
1848 assuming csects can be at arbitrary places in memory and that
1849 the GLOBAL_BLOCK of a symtab has a begin and end address). */
c906108c
SS
1850
1851 /* BEST is the smallest linenumber > LINE so far seen,
c5aa993b
JM
1852 or 0 if none has been seen so far.
1853 BEST_INDEX and BEST_LINETABLE identify the item for it. */
c906108c
SS
1854 int best;
1855
1856 struct objfile *objfile;
1857 struct symtab *s;
1858
1859 if (best_index >= 0)
1860 best = best_linetable->item[best_index].line;
1861 else
1862 best = 0;
1863
1864 ALL_SYMTABS (objfile, s)
c5aa993b
JM
1865 {
1866 struct linetable *l;
1867 int ind;
c906108c 1868
c5aa993b
JM
1869 if (!STREQ (symtab->filename, s->filename))
1870 continue;
1871 l = LINETABLE (s);
1872 ind = find_line_common (l, line, &exact);
1873 if (ind >= 0)
1874 {
1875 if (exact)
1876 {
1877 best_index = ind;
1878 best_linetable = l;
1879 best_symtab = s;
1880 goto done;
1881 }
1882 if (best == 0 || l->item[ind].line < best)
1883 {
1884 best = l->item[ind].line;
1885 best_index = ind;
1886 best_linetable = l;
1887 best_symtab = s;
1888 }
1889 }
1890 }
c906108c 1891 }
c5aa993b 1892done:
c906108c
SS
1893 if (best_index < 0)
1894 return NULL;
1895
1896 if (index)
1897 *index = best_index;
1898 if (exact_match)
1899 *exact_match = exact;
1900
1901 return best_symtab;
1902}
1903\f
1904/* Set the PC value for a given source file and line number and return true.
1905 Returns zero for invalid line number (and sets the PC to 0).
1906 The source file is specified with a struct symtab. */
1907
1908int
fba45db2 1909find_line_pc (struct symtab *symtab, int line, CORE_ADDR *pc)
c906108c
SS
1910{
1911 struct linetable *l;
1912 int ind;
1913
1914 *pc = 0;
1915 if (symtab == 0)
1916 return 0;
1917
1918 symtab = find_line_symtab (symtab, line, &ind, NULL);
1919 if (symtab != NULL)
1920 {
1921 l = LINETABLE (symtab);
1922 *pc = l->item[ind].pc;
1923 return 1;
1924 }
1925 else
1926 return 0;
1927}
1928
1929/* Find the range of pc values in a line.
1930 Store the starting pc of the line into *STARTPTR
1931 and the ending pc (start of next line) into *ENDPTR.
1932 Returns 1 to indicate success.
1933 Returns 0 if could not find the specified line. */
1934
1935int
fba45db2
KB
1936find_line_pc_range (struct symtab_and_line sal, CORE_ADDR *startptr,
1937 CORE_ADDR *endptr)
c906108c
SS
1938{
1939 CORE_ADDR startaddr;
1940 struct symtab_and_line found_sal;
1941
1942 startaddr = sal.pc;
c5aa993b 1943 if (startaddr == 0 && !find_line_pc (sal.symtab, sal.line, &startaddr))
c906108c
SS
1944 return 0;
1945
1946 /* This whole function is based on address. For example, if line 10 has
1947 two parts, one from 0x100 to 0x200 and one from 0x300 to 0x400, then
1948 "info line *0x123" should say the line goes from 0x100 to 0x200
1949 and "info line *0x355" should say the line goes from 0x300 to 0x400.
1950 This also insures that we never give a range like "starts at 0x134
1951 and ends at 0x12c". */
1952
1953 found_sal = find_pc_sect_line (startaddr, sal.section, 0);
1954 if (found_sal.line != sal.line)
1955 {
1956 /* The specified line (sal) has zero bytes. */
1957 *startptr = found_sal.pc;
1958 *endptr = found_sal.pc;
1959 }
1960 else
1961 {
1962 *startptr = found_sal.pc;
1963 *endptr = found_sal.end;
1964 }
1965 return 1;
1966}
1967
1968/* Given a line table and a line number, return the index into the line
1969 table for the pc of the nearest line whose number is >= the specified one.
1970 Return -1 if none is found. The value is >= 0 if it is an index.
1971
1972 Set *EXACT_MATCH nonzero if the value returned is an exact match. */
1973
1974static int
fba45db2
KB
1975find_line_common (register struct linetable *l, register int lineno,
1976 int *exact_match)
c906108c
SS
1977{
1978 register int i;
1979 register int len;
1980
1981 /* BEST is the smallest linenumber > LINENO so far seen,
1982 or 0 if none has been seen so far.
1983 BEST_INDEX identifies the item for it. */
1984
1985 int best_index = -1;
1986 int best = 0;
1987
1988 if (lineno <= 0)
1989 return -1;
1990 if (l == 0)
1991 return -1;
1992
1993 len = l->nitems;
1994 for (i = 0; i < len; i++)
1995 {
1996 register struct linetable_entry *item = &(l->item[i]);
1997
1998 if (item->line == lineno)
1999 {
2000 /* Return the first (lowest address) entry which matches. */
2001 *exact_match = 1;
2002 return i;
2003 }
2004
2005 if (item->line > lineno && (best == 0 || item->line < best))
2006 {
2007 best = item->line;
2008 best_index = i;
2009 }
2010 }
2011
2012 /* If we got here, we didn't get an exact match. */
2013
2014 *exact_match = 0;
2015 return best_index;
2016}
2017
2018int
fba45db2 2019find_pc_line_pc_range (CORE_ADDR pc, CORE_ADDR *startptr, CORE_ADDR *endptr)
c906108c
SS
2020{
2021 struct symtab_and_line sal;
2022 sal = find_pc_line (pc, 0);
2023 *startptr = sal.pc;
2024 *endptr = sal.end;
2025 return sal.symtab != 0;
2026}
2027
2028/* Given a function symbol SYM, find the symtab and line for the start
2029 of the function.
2030 If the argument FUNFIRSTLINE is nonzero, we want the first line
2031 of real code inside the function. */
2032
2033static struct symtab_and_line
a14ed312 2034find_function_start_sal (struct symbol *sym, int);
c906108c
SS
2035
2036static struct symtab_and_line
fba45db2 2037find_function_start_sal (struct symbol *sym, int funfirstline)
c906108c
SS
2038{
2039 CORE_ADDR pc;
2040 struct symtab_and_line sal;
2041
2042 pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
2043 fixup_symbol_section (sym, NULL);
2044 if (funfirstline)
c5aa993b 2045 { /* skip "first line" of function (which is actually its prologue) */
c906108c
SS
2046 asection *section = SYMBOL_BFD_SECTION (sym);
2047 /* If function is in an unmapped overlay, use its unmapped LMA
c5aa993b 2048 address, so that SKIP_PROLOGUE has something unique to work on */
c906108c
SS
2049 if (section_is_overlay (section) &&
2050 !section_is_mapped (section))
2051 pc = overlay_unmapped_address (pc, section);
2052
2053 pc += FUNCTION_START_OFFSET;
b83266a0 2054 pc = SKIP_PROLOGUE (pc);
c906108c
SS
2055
2056 /* For overlays, map pc back into its mapped VMA range */
2057 pc = overlay_mapped_address (pc, section);
2058 }
2059 sal = find_pc_sect_line (pc, SYMBOL_BFD_SECTION (sym), 0);
2060
2061#ifdef PROLOGUE_FIRSTLINE_OVERLAP
2062 /* Convex: no need to suppress code on first line, if any */
2063 sal.pc = pc;
2064#else
2065 /* Check if SKIP_PROLOGUE left us in mid-line, and the next
2066 line is still part of the same function. */
2067 if (sal.pc != pc
2068 && BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) <= sal.end
2069 && sal.end < BLOCK_END (SYMBOL_BLOCK_VALUE (sym)))
2070 {
2071 /* First pc of next line */
2072 pc = sal.end;
2073 /* Recalculate the line number (might not be N+1). */
2074 sal = find_pc_sect_line (pc, SYMBOL_BFD_SECTION (sym), 0);
2075 }
2076 sal.pc = pc;
2077#endif
2078
2079 return sal;
2080}
2081\f
2082/* If P is of the form "operator[ \t]+..." where `...' is
2083 some legitimate operator text, return a pointer to the
2084 beginning of the substring of the operator text.
2085 Otherwise, return "". */
2086char *
fba45db2 2087operator_chars (char *p, char **end)
c906108c
SS
2088{
2089 *end = "";
2090 if (strncmp (p, "operator", 8))
2091 return *end;
2092 p += 8;
2093
2094 /* Don't get faked out by `operator' being part of a longer
2095 identifier. */
c5aa993b 2096 if (isalpha (*p) || *p == '_' || *p == '$' || *p == '\0')
c906108c
SS
2097 return *end;
2098
2099 /* Allow some whitespace between `operator' and the operator symbol. */
2100 while (*p == ' ' || *p == '\t')
2101 p++;
2102
2103 /* Recognize 'operator TYPENAME'. */
2104
c5aa993b 2105 if (isalpha (*p) || *p == '_' || *p == '$')
c906108c 2106 {
c5aa993b
JM
2107 register char *q = p + 1;
2108 while (isalnum (*q) || *q == '_' || *q == '$')
c906108c
SS
2109 q++;
2110 *end = q;
2111 return p;
2112 }
2113
2114 switch (*p)
2115 {
2116 case '!':
2117 case '=':
2118 case '*':
2119 case '/':
2120 case '%':
2121 case '^':
2122 if (p[1] == '=')
c5aa993b 2123 *end = p + 2;
c906108c 2124 else
c5aa993b 2125 *end = p + 1;
c906108c
SS
2126 return p;
2127 case '<':
2128 case '>':
2129 case '+':
2130 case '-':
2131 case '&':
2132 case '|':
2133 if (p[1] == '=' || p[1] == p[0])
c5aa993b 2134 *end = p + 2;
c906108c 2135 else
c5aa993b 2136 *end = p + 1;
c906108c
SS
2137 return p;
2138 case '~':
2139 case ',':
c5aa993b 2140 *end = p + 1;
c906108c
SS
2141 return p;
2142 case '(':
2143 if (p[1] != ')')
2144 error ("`operator ()' must be specified without whitespace in `()'");
c5aa993b 2145 *end = p + 2;
c906108c
SS
2146 return p;
2147 case '?':
2148 if (p[1] != ':')
2149 error ("`operator ?:' must be specified without whitespace in `?:'");
c5aa993b 2150 *end = p + 2;
c906108c
SS
2151 return p;
2152 case '[':
2153 if (p[1] != ']')
2154 error ("`operator []' must be specified without whitespace in `[]'");
c5aa993b 2155 *end = p + 2;
c906108c
SS
2156 return p;
2157 default:
2158 error ("`operator %s' not supported", p);
2159 break;
2160 }
2161 *end = "";
2162 return *end;
2163}
2164
2165/* Return the number of methods described for TYPE, including the
2166 methods from types it derives from. This can't be done in the symbol
2167 reader because the type of the baseclass might still be stubbed
2168 when the definition of the derived class is parsed. */
2169
a14ed312 2170static int total_number_of_methods (struct type *type);
c906108c
SS
2171
2172static int
fba45db2 2173total_number_of_methods (struct type *type)
c906108c
SS
2174{
2175 int n;
2176 int count;
2177
2178 CHECK_TYPEDEF (type);
2179 if (TYPE_CPLUS_SPECIFIC (type) == NULL)
2180 return 0;
2181 count = TYPE_NFN_FIELDS_TOTAL (type);
2182
2183 for (n = 0; n < TYPE_N_BASECLASSES (type); n++)
2184 count += total_number_of_methods (TYPE_BASECLASS (type, n));
2185
2186 return count;
2187}
2188
2189/* Recursive helper function for decode_line_1.
2190 Look for methods named NAME in type T.
2191 Return number of matches.
2192 Put matches in SYM_ARR, which should have been allocated with
2193 a size of total_number_of_methods (T) * sizeof (struct symbol *).
2194 Note that this function is g++ specific. */
2195
2196static int
fba45db2 2197find_methods (struct type *t, char *name, struct symbol **sym_arr)
c906108c
SS
2198{
2199 int i1 = 0;
2200 int ibase;
2201 struct symbol *sym_class;
2202 char *class_name = type_name_no_tag (t);
2203
2204 /* Ignore this class if it doesn't have a name. This is ugly, but
2205 unless we figure out how to get the physname without the name of
2206 the class, then the loop can't do any good. */
2207 if (class_name
2208 && (sym_class = lookup_symbol (class_name,
c5aa993b 2209 (struct block *) NULL,
c906108c 2210 STRUCT_NAMESPACE,
c5aa993b
JM
2211 (int *) NULL,
2212 (struct symtab **) NULL)))
c906108c
SS
2213 {
2214 int method_counter;
2215
2216 /* FIXME: Shouldn't this just be CHECK_TYPEDEF (t)? */
2217 t = SYMBOL_TYPE (sym_class);
2218
2219 /* Loop over each method name. At this level, all overloads of a name
c5aa993b
JM
2220 are counted as a single name. There is an inner loop which loops over
2221 each overload. */
c906108c
SS
2222
2223 for (method_counter = TYPE_NFN_FIELDS (t) - 1;
2224 method_counter >= 0;
2225 --method_counter)
2226 {
2227 int field_counter;
2228 char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
2229 char dem_opname[64];
2230
c5aa993b 2231 if (strncmp (method_name, "__", 2) == 0 ||
c906108c
SS
2232 strncmp (method_name, "op", 2) == 0 ||
2233 strncmp (method_name, "type", 4) == 0)
c5aa993b 2234 {
c906108c 2235 if (cplus_demangle_opname (method_name, dem_opname, DMGL_ANSI))
c5aa993b 2236 method_name = dem_opname;
c906108c 2237 else if (cplus_demangle_opname (method_name, dem_opname, 0))
c5aa993b
JM
2238 method_name = dem_opname;
2239 }
c906108c
SS
2240
2241 if (STREQ (name, method_name))
2242 /* Find all the overloaded methods with that name. */
2243 for (field_counter = TYPE_FN_FIELDLIST_LENGTH (t, method_counter) - 1;
2244 field_counter >= 0;
2245 --field_counter)
2246 {
2247 struct fn_field *f;
2248 char *phys_name;
2249
2250 f = TYPE_FN_FIELDLIST1 (t, method_counter);
2251
2252 if (TYPE_FN_FIELD_STUB (f, field_counter))
2253 {
2254 char *tmp_name;
2255
2256 tmp_name = gdb_mangle_name (t,
c5aa993b
JM
2257 method_counter,
2258 field_counter);
c906108c
SS
2259 phys_name = alloca (strlen (tmp_name) + 1);
2260 strcpy (phys_name, tmp_name);
2261 free (tmp_name);
2262 }
2263 else
2264 phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
2265
2266 /* Destructor is handled by caller, dont add it to the list */
2267 if (DESTRUCTOR_PREFIX_P (phys_name))
2268 continue;
2269
2270 sym_arr[i1] = lookup_symbol (phys_name,
2271 NULL, VAR_NAMESPACE,
2272 (int *) NULL,
2273 (struct symtab **) NULL);
2274 if (sym_arr[i1])
2275 i1++;
2276 else
2277 {
2278 /* This error message gets printed, but the method
2279 still seems to be found
2280 fputs_filtered("(Cannot find method ", gdb_stdout);
2281 fprintf_symbol_filtered (gdb_stdout, phys_name,
2282 language_cplus,
2283 DMGL_PARAMS | DMGL_ANSI);
2284 fputs_filtered(" - possibly inlined.)\n", gdb_stdout);
c5aa993b 2285 */
c906108c
SS
2286 }
2287 }
2288 }
2289 }
2290
2291 /* Only search baseclasses if there is no match yet, since names in
2292 derived classes override those in baseclasses.
2293
2294 FIXME: The above is not true; it is only true of member functions
2295 if they have the same number of arguments (??? - section 13.1 of the
2296 ARM says the function members are not in the same scope but doesn't
2297 really spell out the rules in a way I understand. In any case, if
2298 the number of arguments differ this is a case in which we can overload
2299 rather than hiding without any problem, and gcc 2.4.5 does overload
2300 rather than hiding in this case). */
2301
2302 if (i1 == 0)
2303 for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
2304 i1 += find_methods (TYPE_BASECLASS (t, ibase), name, sym_arr + i1);
2305
2306 return i1;
2307}
2308
2309/* Helper function for decode_line_1.
2310 Build a canonical line spec in CANONICAL if it is non-NULL and if
2311 the SAL has a symtab.
2312 If SYMNAME is non-NULL the canonical line spec is `filename:symname'.
2313 If SYMNAME is NULL the line number from SAL is used and the canonical
2314 line spec is `filename:linenum'. */
2315
2316static void
fba45db2
KB
2317build_canonical_line_spec (struct symtab_and_line *sal, char *symname,
2318 char ***canonical)
c906108c
SS
2319{
2320 char **canonical_arr;
2321 char *canonical_name;
2322 char *filename;
2323 struct symtab *s = sal->symtab;
2324
c5aa993b
JM
2325 if (s == (struct symtab *) NULL
2326 || s->filename == (char *) NULL
2327 || canonical == (char ***) NULL)
c906108c 2328 return;
c5aa993b 2329
c906108c
SS
2330 canonical_arr = (char **) xmalloc (sizeof (char *));
2331 *canonical = canonical_arr;
2332
2333 filename = s->filename;
2334 if (symname != NULL)
2335 {
2336 canonical_name = xmalloc (strlen (filename) + strlen (symname) + 2);
2337 sprintf (canonical_name, "%s:%s", filename, symname);
2338 }
2339 else
2340 {
2341 canonical_name = xmalloc (strlen (filename) + 30);
2342 sprintf (canonical_name, "%s:%d", filename, sal->line);
2343 }
2344 canonical_arr[0] = canonical_name;
2345}
2346
da59e081
JM
2347
2348
2349/* Find an instance of the character C in the string S that is outside
2350 of all parenthesis pairs, single-quoted strings, and double-quoted
2351 strings. */
2352static char *
2353find_toplevel_char (char *s, char c)
2354{
2355 int quoted = 0; /* zero if we're not in quotes;
2356 '"' if we're in a double-quoted string;
2357 '\'' if we're in a single-quoted string. */
2358 int depth = 0; /* number of unclosed parens we've seen */
2359 char *scan;
2360
2361 for (scan = s; *scan; scan++)
2362 {
2363 if (quoted)
2364 {
2365 if (*scan == quoted)
2366 quoted = 0;
2367 else if (*scan == '\\' && *(scan + 1))
2368 scan++;
2369 }
2370 else if (*scan == c && ! quoted && depth == 0)
2371 return scan;
2372 else if (*scan == '"' || *scan == '\'')
2373 quoted = *scan;
2374 else if (*scan == '(')
2375 depth++;
2376 else if (*scan == ')' && depth > 0)
2377 depth--;
2378 }
2379
2380 return 0;
2381}
2382
2383
c906108c
SS
2384/* Parse a string that specifies a line number.
2385 Pass the address of a char * variable; that variable will be
2386 advanced over the characters actually parsed.
2387
2388 The string can be:
2389
2390 LINENUM -- that line number in current file. PC returned is 0.
2391 FILE:LINENUM -- that line in that file. PC returned is 0.
2392 FUNCTION -- line number of openbrace of that function.
c5aa993b 2393 PC returned is the start of the function.
c906108c 2394 VARIABLE -- line number of definition of that variable.
c5aa993b 2395 PC returned is 0.
c906108c
SS
2396 FILE:FUNCTION -- likewise, but prefer functions in that file.
2397 *EXPR -- line in which address EXPR appears.
2398
085dd6e6
JM
2399 This may all be followed by an "if EXPR", which we ignore.
2400
c906108c
SS
2401 FUNCTION may be an undebuggable function found in minimal symbol table.
2402
2403 If the argument FUNFIRSTLINE is nonzero, we want the first line
2404 of real code inside a function when a function is specified, and it is
2405 not OK to specify a variable or type to get its line number.
2406
2407 DEFAULT_SYMTAB specifies the file to use if none is specified.
2408 It defaults to current_source_symtab.
2409 DEFAULT_LINE specifies the line number to use for relative
2410 line numbers (that start with signs). Defaults to current_source_line.
2411 If CANONICAL is non-NULL, store an array of strings containing the canonical
2412 line specs there if necessary. Currently overloaded member functions and
2413 line numbers or static functions without a filename yield a canonical
2414 line spec. The array and the line spec strings are allocated on the heap,
2415 it is the callers responsibility to free them.
2416
2417 Note that it is possible to return zero for the symtab
2418 if no file is validly specified. Callers must check that.
2419 Also, the line number returned may be invalid. */
2420
2421/* We allow single quotes in various places. This is a hideous
2422 kludge, which exists because the completer can't yet deal with the
2423 lack of single quotes. FIXME: write a linespec_completer which we
2424 can use as appropriate instead of make_symbol_completion_list. */
2425
2426struct symtabs_and_lines
fba45db2
KB
2427decode_line_1 (char **argptr, int funfirstline, struct symtab *default_symtab,
2428 int default_line, char ***canonical)
c906108c
SS
2429{
2430 struct symtabs_and_lines values;
2431#ifdef HPPA_COMPILER_BUG
2432 /* FIXME: The native HP 9000/700 compiler has a bug which appears
2433 when optimizing this file with target i960-vxworks. I haven't
2434 been able to construct a simple test case. The problem is that
2435 in the second call to SKIP_PROLOGUE below, the compiler somehow
2436 does not realize that the statement val = find_pc_line (...) will
2437 change the values of the fields of val. It extracts the elements
2438 into registers at the top of the block, and does not update the
2439 registers after the call to find_pc_line. You can check this by
2440 inserting a printf at the end of find_pc_line to show what values
2441 it is returning for val.pc and val.end and another printf after
2442 the call to see what values the function actually got (remember,
2443 this is compiling with cc -O, with this patch removed). You can
2444 also examine the assembly listing: search for the second call to
2445 skip_prologue; the LDO statement before the next call to
2446 find_pc_line loads the address of the structure which
2447 find_pc_line will return; if there is a LDW just before the LDO,
2448 which fetches an element of the structure, then the compiler
2449 still has the bug.
2450
2451 Setting val to volatile avoids the problem. We must undef
2452 volatile, because the HPPA native compiler does not define
2453 __STDC__, although it does understand volatile, and so volatile
2454 will have been defined away in defs.h. */
2455#undef volatile
2456 volatile struct symtab_and_line val;
c5aa993b 2457#define volatile /*nothing */
c906108c
SS
2458#else
2459 struct symtab_and_line val;
2460#endif
2461 register char *p, *p1;
2462 char *q, *pp, *ii, *p2;
2463#if 0
2464 char *q1;
2465#endif
2466 register struct symtab *s;
2467
2468 register struct symbol *sym;
2469 /* The symtab that SYM was found in. */
2470 struct symtab *sym_symtab;
2471
2472 register CORE_ADDR pc;
2473 register struct minimal_symbol *msymbol;
2474 char *copy;
2475 struct symbol *sym_class;
2476 int i1;
2477 int is_quoted;
cce74817 2478 int is_quote_enclosed;
c5aa993b 2479 int has_parens;
c906108c 2480 int has_if = 0;
cce74817 2481 int has_comma = 0;
c906108c
SS
2482 struct symbol **sym_arr;
2483 struct type *t;
2484 char *saved_arg = *argptr;
2485 extern char *gdb_completer_quote_characters;
c5aa993b
JM
2486
2487 INIT_SAL (&val); /* initialize to zeroes */
c906108c
SS
2488
2489 /* Defaults have defaults. */
2490
2491 if (default_symtab == 0)
2492 {
2493 default_symtab = current_source_symtab;
2494 default_line = current_source_line;
2495 }
2496
2497 /* See if arg is *PC */
2498
2499 if (**argptr == '*')
2500 {
2501 (*argptr)++;
2502 pc = parse_and_eval_address_1 (argptr);
2503
2504 values.sals = (struct symtab_and_line *)
2505 xmalloc (sizeof (struct symtab_and_line));
2506
2507 values.nelts = 1;
2508 values.sals[0] = find_pc_line (pc, 0);
2509 values.sals[0].pc = pc;
2510 values.sals[0].section = find_pc_overlay (pc);
2511
2512 return values;
2513 }
2514
2515 /* 'has_if' is for the syntax:
2516 * (gdb) break foo if (a==b)
2517 */
c5aa993b
JM
2518 if ((ii = strstr (*argptr, " if ")) != NULL ||
2519 (ii = strstr (*argptr, "\tif ")) != NULL ||
2520 (ii = strstr (*argptr, " if\t")) != NULL ||
2521 (ii = strstr (*argptr, "\tif\t")) != NULL ||
2522 (ii = strstr (*argptr, " if(")) != NULL ||
2523 (ii = strstr (*argptr, "\tif( ")) != NULL)
c906108c
SS
2524 has_if = 1;
2525 /* Temporarily zap out "if (condition)" to not
2526 * confuse the parenthesis-checking code below.
2527 * This is undone below. Do not change ii!!
2528 */
c5aa993b
JM
2529 if (has_if)
2530 {
2531 *ii = '\0';
2532 }
c906108c
SS
2533
2534 /* Set various flags.
2535 * 'has_parens' is important for overload checking, where
2536 * we allow things like:
2537 * (gdb) break c::f(int)
2538 */
2539
2540 /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
2541
2542 is_quoted = (**argptr
2543 && strchr (gdb_completer_quote_characters, **argptr) != NULL);
2544
2545 has_parens = ((pp = strchr (*argptr, '(')) != NULL
c2c6d25f 2546 && (pp = strrchr (pp, ')')) != NULL);
c906108c
SS
2547
2548 /* Now that we're safely past the has_parens check,
2549 * put back " if (condition)" so outer layers can see it
2550 */
2551 if (has_if)
2552 *ii = ' ';
2553
cce74817
JM
2554 /* Maybe we were called with a line range FILENAME:LINENUM,FILENAME:LINENUM
2555 and we must isolate the first half. Outer layers will call again later
da59e081
JM
2556 for the second half.
2557
2558 Don't count commas that appear in argument lists of overloaded
2559 functions, or in quoted strings. It's stupid to go to this much
2560 trouble when the rest of the function is such an obvious roach hotel. */
2561 ii = find_toplevel_char (*argptr, ',');
2562 has_comma = (ii != 0);
2563
cce74817
JM
2564 /* Temporarily zap out second half to not
2565 * confuse the code below.
2566 * This is undone below. Do not change ii!!
2567 */
c5aa993b
JM
2568 if (has_comma)
2569 {
2570 *ii = '\0';
2571 }
cce74817 2572
c906108c
SS
2573 /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
2574 /* May also be CLASS::MEMBER, or NAMESPACE::NAME */
2575 /* Look for ':', but ignore inside of <> */
2576
2577 s = NULL;
cce74817
JM
2578 p = *argptr;
2579 if (p[0] == '"')
2580 {
2581 is_quote_enclosed = 1;
2582 p++;
2583 }
2584 else
c5aa993b
JM
2585 is_quote_enclosed = 0;
2586 for (; *p; p++)
c906108c 2587 {
c5aa993b 2588 if (p[0] == '<')
c906108c 2589 {
c5aa993b
JM
2590 char *temp_end = find_template_name_end (p);
2591 if (!temp_end)
2592 error ("malformed template specification in command");
2593 p = temp_end;
c906108c 2594 }
cce74817
JM
2595 /* Check for the end of the first half of the linespec. End of line,
2596 a tab, a double colon or the last single colon, or a space. But
2597 if enclosed in double quotes we do not break on enclosed spaces */
2598 if (!*p
c5aa993b
JM
2599 || p[0] == '\t'
2600 || ((p[0] == ':')
2601 && ((p[1] == ':') || (strchr (p + 1, ':') == NULL)))
2602 || ((p[0] == ' ') && !is_quote_enclosed))
2603 break;
2604 if (p[0] == '.' && strchr (p, ':') == NULL) /* Java qualified method. */
c906108c
SS
2605 {
2606 /* Find the *last* '.', since the others are package qualifiers. */
c5aa993b 2607 for (p1 = p; *p1; p1++)
c906108c
SS
2608 {
2609 if (*p1 == '.')
2610 p = p1;
2611 }
2612 break;
2613 }
2614 }
c5aa993b
JM
2615 while (p[0] == ' ' || p[0] == '\t')
2616 p++;
da59e081 2617
cce74817 2618 /* if the closing double quote was left at the end, remove it */
da59e081
JM
2619 if (is_quote_enclosed)
2620 {
2621 char *closing_quote = strchr (p, '"');
2622 if (closing_quote && closing_quote[1] == '\0')
2623 *closing_quote = '\0';
2624 }
cce74817
JM
2625
2626 /* Now that we've safely parsed the first half,
2627 * put back ',' so outer layers can see it
2628 */
2629 if (has_comma)
2630 *ii = ',';
c906108c
SS
2631
2632 if ((p[0] == ':' || p[0] == '.') && !has_parens)
2633 {
2634 /* C++ */
2635 /* ... or Java */
c5aa993b
JM
2636 if (is_quoted)
2637 *argptr = *argptr + 1;
2638 if (p[0] == '.' || p[1] == ':')
c906108c 2639 {
c5aa993b
JM
2640 char *saved_arg2 = *argptr;
2641 char *temp_end;
2642 /* First check for "global" namespace specification,
2643 of the form "::foo". If found, skip over the colons
2644 and jump to normal symbol processing */
2645 if ((*argptr == p) || (p[-1] == ' ') || (p[-1] == '\t'))
2646 saved_arg2 += 2;
2647
2648 /* We have what looks like a class or namespace
2649 scope specification (A::B), possibly with many
2650 levels of namespaces or classes (A::B::C::D).
2651
2652 Some versions of the HP ANSI C++ compiler (as also possibly
2653 other compilers) generate class/function/member names with
2654 embedded double-colons if they are inside namespaces. To
2655 handle this, we loop a few times, considering larger and
2656 larger prefixes of the string as though they were single
2657 symbols. So, if the initially supplied string is
2658 A::B::C::D::foo, we have to look up "A", then "A::B",
2659 then "A::B::C", then "A::B::C::D", and finally
2660 "A::B::C::D::foo" as single, monolithic symbols, because
2661 A, B, C or D may be namespaces.
2662
2663 Note that namespaces can nest only inside other
2664 namespaces, and not inside classes. So we need only
2665 consider *prefixes* of the string; there is no need to look up
2666 "B::C" separately as a symbol in the previous example. */
2667
2668 p2 = p; /* save for restart */
2669 while (1)
2670 {
2671 /* Extract the class name. */
2672 p1 = p;
2673 while (p != *argptr && p[-1] == ' ')
2674 --p;
2675 copy = (char *) alloca (p - *argptr + 1);
2676 memcpy (copy, *argptr, p - *argptr);
2677 copy[p - *argptr] = 0;
2678
2679 /* Discard the class name from the arg. */
2680 p = p1 + (p1[0] == ':' ? 2 : 1);
2681 while (*p == ' ' || *p == '\t')
2682 p++;
2683 *argptr = p;
2684
2685 sym_class = lookup_symbol (copy, 0, STRUCT_NAMESPACE, 0,
2686 (struct symtab **) NULL);
2687
2688 if (sym_class &&
2689 (t = check_typedef (SYMBOL_TYPE (sym_class)),
2690 (TYPE_CODE (t) == TYPE_CODE_STRUCT
2691 || TYPE_CODE (t) == TYPE_CODE_UNION)))
c906108c 2692 {
c5aa993b
JM
2693 /* Arg token is not digits => try it as a function name
2694 Find the next token(everything up to end or next blank). */
2695 if (**argptr
2696 && strchr (gdb_completer_quote_characters, **argptr) != NULL)
2697 {
2698 p = skip_quoted (*argptr);
2699 *argptr = *argptr + 1;
2700 }
2701 else
2702 {
2703 p = *argptr;
2704 while (*p && *p != ' ' && *p != '\t' && *p != ',' && *p != ':')
2705 p++;
2706 }
2707/*
2708 q = operator_chars (*argptr, &q1);
2709 if (q1 - q)
2710 {
2711 char *opname;
2712 char *tmp = alloca (q1 - q + 1);
2713 memcpy (tmp, q, q1 - q);
2714 tmp[q1 - q] = '\0';
2715 opname = cplus_mangle_opname (tmp, DMGL_ANSI);
2716 if (opname == NULL)
2717 {
2718 error_begin ();
2719 printf_filtered ("no mangling for \"%s\"\n", tmp);
2720 cplusplus_hint (saved_arg);
2721 return_to_top_level (RETURN_ERROR);
2722 }
2723 copy = (char*) alloca (3 + strlen(opname));
2724 sprintf (copy, "__%s", opname);
2725 p = q1;
2726 }
2727 else
2728 */
2729 {
2730 copy = (char *) alloca (p - *argptr + 1);
2731 memcpy (copy, *argptr, p - *argptr);
2732 copy[p - *argptr] = '\0';
2733 if (p != *argptr
2734 && copy[p - *argptr - 1]
2735 && strchr (gdb_completer_quote_characters,
2736 copy[p - *argptr - 1]) != NULL)
2737 copy[p - *argptr - 1] = '\0';
2738 }
2739
2740 /* no line number may be specified */
2741 while (*p == ' ' || *p == '\t')
2742 p++;
2743 *argptr = p;
2744
2745 sym = 0;
2746 i1 = 0; /* counter for the symbol array */
2747 sym_arr = (struct symbol **) alloca (total_number_of_methods (t)
2748 * sizeof (struct symbol *));
2749
2750 if (destructor_name_p (copy, t))
c906108c 2751 {
c5aa993b
JM
2752 /* Destructors are a special case. */
2753 int m_index, f_index;
2754
2755 if (get_destructor_fn_field (t, &m_index, &f_index))
2756 {
2757 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, m_index);
2758
2759 sym_arr[i1] =
2760 lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, f_index),
2761 NULL, VAR_NAMESPACE, (int *) NULL,
2762 (struct symtab **) NULL);
2763 if (sym_arr[i1])
2764 i1++;
2765 }
2766 }
2767 else
2768 i1 = find_methods (t, copy, sym_arr);
2769 if (i1 == 1)
2770 {
2771 /* There is exactly one field with that name. */
2772 sym = sym_arr[0];
2773
2774 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2775 {
2776 values.sals = (struct symtab_and_line *)
2777 xmalloc (sizeof (struct symtab_and_line));
2778 values.nelts = 1;
2779 values.sals[0] = find_function_start_sal (sym,
2780 funfirstline);
2781 }
2782 else
2783 {
2784 values.nelts = 0;
2785 }
2786 return values;
2787 }
2788 if (i1 > 0)
2789 {
2790 /* There is more than one field with that name
2791 (overloaded). Ask the user which one to use. */
2792 return decode_line_2 (sym_arr, i1, funfirstline, canonical);
2793 }
2794 else
2795 {
2796 char *tmp;
2797
2798 if (OPNAME_PREFIX_P (copy))
2799 {
2800 tmp = (char *) alloca (strlen (copy + 3) + 9);
2801 strcpy (tmp, "operator ");
2802 strcat (tmp, copy + 3);
2803 }
2804 else
2805 tmp = copy;
c906108c 2806 error_begin ();
c5aa993b
JM
2807 if (tmp[0] == '~')
2808 printf_filtered
2809 ("the class `%s' does not have destructor defined\n",
2810 SYMBOL_SOURCE_NAME (sym_class));
2811 else
2812 printf_filtered
2813 ("the class %s does not have any method named %s\n",
2814 SYMBOL_SOURCE_NAME (sym_class), tmp);
c906108c
SS
2815 cplusplus_hint (saved_arg);
2816 return_to_top_level (RETURN_ERROR);
2817 }
c906108c 2818 }
c5aa993b
JM
2819
2820 /* Move pointer up to next possible class/namespace token */
2821 p = p2 + 1; /* restart with old value +1 */
2822 /* Move pointer ahead to next double-colon */
2823 while (*p && (p[0] != ' ') && (p[0] != '\t') && (p[0] != '\''))
2824 {
2825 if (p[0] == '<')
2826 {
2827 temp_end = find_template_name_end (p);
2828 if (!temp_end)
2829 error ("malformed template specification in command");
2830 p = temp_end;
2831 }
2832 else if ((p[0] == ':') && (p[1] == ':'))
2833 break; /* found double-colon */
2834 else
2835 p++;
2836 }
2837
2838 if (*p != ':')
2839 break; /* out of the while (1) */
2840
2841 p2 = p; /* save restart for next time around */
2842 *argptr = saved_arg2; /* restore argptr */
2843 } /* while (1) */
2844
2845 /* Last chance attempt -- check entire name as a symbol */
2846 /* Use "copy" in preparation for jumping out of this block,
2847 to be consistent with usage following the jump target */
2848 copy = (char *) alloca (p - saved_arg2 + 1);
2849 memcpy (copy, saved_arg2, p - saved_arg2);
2850 /* Note: if is_quoted should be true, we snuff out quote here anyway */
2851 copy[p - saved_arg2] = '\000';
2852 /* Set argptr to skip over the name */
2853 *argptr = (*p == '\'') ? p + 1 : p;
2854 /* Look up entire name */
2855 sym = lookup_symbol (copy, 0, VAR_NAMESPACE, 0, &sym_symtab);
2856 s = (struct symtab *) 0;
2857 /* Prepare to jump: restore the " if (condition)" so outer layers see it */
2858 /* Symbol was found --> jump to normal symbol processing.
2859 Code following "symbol_found" expects "copy" to have the
2860 symbol name, "sym" to have the symbol pointer, "s" to be
2861 a specified file's symtab, and sym_symtab to be the symbol's
2862 symtab. */
2863 /* By jumping there we avoid falling through the FILE:LINE and
2864 FILE:FUNC processing stuff below */
2865 if (sym)
2866 goto symbol_found;
2867
2868 /* Couldn't find any interpretation as classes/namespaces, so give up */
2869 error_begin ();
2870 /* The quotes are important if copy is empty. */
2871 printf_filtered
2872 ("Can't find member of namespace, class, struct, or union named \"%s\"\n", copy);
2873 cplusplus_hint (saved_arg);
2874 return_to_top_level (RETURN_ERROR);
2875 }
c906108c
SS
2876 /* end of C++ */
2877
2878
2879 /* Extract the file name. */
2880 p1 = p;
c5aa993b
JM
2881 while (p != *argptr && p[-1] == ' ')
2882 --p;
2883 if ((*p == '"') && is_quote_enclosed)
2884 --p;
c906108c 2885 copy = (char *) alloca (p - *argptr + 1);
cce74817 2886 if ((**argptr == '"') && is_quote_enclosed)
c5aa993b
JM
2887 {
2888 memcpy (copy, *argptr + 1, p - *argptr - 1);
2889 /* It may have the ending quote right after the file name */
2890 if (copy[p - *argptr - 2] == '"')
2891 copy[p - *argptr - 2] = 0;
2892 else
2893 copy[p - *argptr - 1] = 0;
2894 }
cce74817 2895 else
c5aa993b
JM
2896 {
2897 memcpy (copy, *argptr, p - *argptr);
2898 copy[p - *argptr] = 0;
2899 }
c906108c
SS
2900
2901 /* Find that file's data. */
2902 s = lookup_symtab (copy);
2903 if (s == 0)
2904 {
2905 if (!have_full_symbols () && !have_partial_symbols ())
2906 error (no_symtab_msg);
2907 error ("No source file named %s.", copy);
2908 }
2909
2910 /* Discard the file name from the arg. */
2911 p = p1 + 1;
c5aa993b
JM
2912 while (*p == ' ' || *p == '\t')
2913 p++;
c906108c
SS
2914 *argptr = p;
2915 }
7a292a7a
SS
2916#if 0
2917 /* No one really seems to know why this was added. It certainly
2918 breaks the command line, though, whenever the passed
2919 name is of the form ClassName::Method. This bit of code
2920 singles out the class name, and if funfirstline is set (for
2921 example, you are setting a breakpoint at this function),
2922 you get an error. This did not occur with earlier
2923 verions, so I am ifdef'ing this out. 3/29/99 */
c5aa993b
JM
2924 else
2925 {
2926 /* Check if what we have till now is a symbol name */
2927
2928 /* We may be looking at a template instantiation such
2929 as "foo<int>". Check here whether we know about it,
2930 instead of falling through to the code below which
2931 handles ordinary function names, because that code
2932 doesn't like seeing '<' and '>' in a name -- the
2933 skip_quoted call doesn't go past them. So see if we
2934 can figure it out right now. */
2935
2936 copy = (char *) alloca (p - *argptr + 1);
2937 memcpy (copy, *argptr, p - *argptr);
2938 copy[p - *argptr] = '\000';
2939 sym = lookup_symbol (copy, 0, VAR_NAMESPACE, 0, &sym_symtab);
2940 if (sym)
2941 {
2942 /* Yes, we have a symbol; jump to symbol processing */
2943 /* Code after symbol_found expects S, SYM_SYMTAB, SYM,
2944 and COPY to be set correctly */
2945 *argptr = (*p == '\'') ? p + 1 : p;
2946 s = (struct symtab *) 0;
2947 goto symbol_found;
2948 }
2949 /* Otherwise fall out from here and go to file/line spec
2950 processing, etc. */
c906108c 2951 }
7a292a7a 2952#endif
c906108c
SS
2953
2954 /* S is specified file's symtab, or 0 if no file specified.
2955 arg no longer contains the file name. */
2956
2957 /* Check whether arg is all digits (and sign) */
2958
2959 q = *argptr;
c5aa993b
JM
2960 if (*q == '-' || *q == '+')
2961 q++;
c906108c
SS
2962 while (*q >= '0' && *q <= '9')
2963 q++;
2964
2965 if (q != *argptr && (*q == 0 || *q == ' ' || *q == '\t' || *q == ','))
2966 {
2967 /* We found a token consisting of all digits -- at least one digit. */
c5aa993b
JM
2968 enum sign
2969 {
2970 none, plus, minus
2971 }
2972 sign = none;
c906108c
SS
2973
2974 /* We might need a canonical line spec if no file was specified. */
2975 int need_canonical = (s == 0) ? 1 : 0;
2976
2977 /* This is where we need to make sure that we have good defaults.
c5aa993b
JM
2978 We must guarantee that this section of code is never executed
2979 when we are called with just a function name, since
2980 select_source_symtab calls us with such an argument */
c906108c
SS
2981
2982 if (s == 0 && default_symtab == 0)
2983 {
2984 select_source_symtab (0);
2985 default_symtab = current_source_symtab;
2986 default_line = current_source_line;
2987 }
2988
2989 if (**argptr == '+')
2990 sign = plus, (*argptr)++;
2991 else if (**argptr == '-')
2992 sign = minus, (*argptr)++;
2993 val.line = atoi (*argptr);
2994 switch (sign)
2995 {
2996 case plus:
2997 if (q == *argptr)
2998 val.line = 5;
2999 if (s == 0)
3000 val.line = default_line + val.line;
3001 break;
3002 case minus:
3003 if (q == *argptr)
3004 val.line = 15;
3005 if (s == 0)
3006 val.line = default_line - val.line;
3007 else
3008 val.line = 1;
3009 break;
3010 case none:
c5aa993b 3011 break; /* No need to adjust val.line. */
c906108c
SS
3012 }
3013
c5aa993b
JM
3014 while (*q == ' ' || *q == '\t')
3015 q++;
c906108c
SS
3016 *argptr = q;
3017 if (s == 0)
3018 s = default_symtab;
3019
3020 /* It is possible that this source file has more than one symtab,
c5aa993b
JM
3021 and that the new line number specification has moved us from the
3022 default (in s) to a new one. */
c906108c
SS
3023 val.symtab = find_line_symtab (s, val.line, NULL, NULL);
3024 if (val.symtab == 0)
3025 val.symtab = s;
c5aa993b 3026
c906108c
SS
3027 val.pc = 0;
3028 values.sals = (struct symtab_and_line *)
3029 xmalloc (sizeof (struct symtab_and_line));
3030 values.sals[0] = val;
3031 values.nelts = 1;
3032 if (need_canonical)
3033 build_canonical_line_spec (values.sals, NULL, canonical);
3034 return values;
3035 }
3036
3037 /* Arg token is not digits => try it as a variable name
3038 Find the next token (everything up to end or next whitespace). */
3039
3040 if (**argptr == '$') /* May be a convenience variable */
c5aa993b 3041 p = skip_quoted (*argptr + (((*argptr)[1] == '$') ? 2 : 1)); /* One or two $ chars possible */
c906108c
SS
3042 else if (is_quoted)
3043 {
3044 p = skip_quoted (*argptr);
3045 if (p[-1] != '\'')
c5aa993b 3046 error ("Unmatched single quote.");
c906108c
SS
3047 }
3048 else if (has_parens)
3049 {
c5aa993b 3050 p = pp + 1;
c906108c 3051 }
c5aa993b 3052 else
c906108c 3053 {
c5aa993b 3054 p = skip_quoted (*argptr);
c906108c
SS
3055 }
3056
da59e081
JM
3057 if (is_quote_enclosed && **argptr == '"')
3058 (*argptr)++;
3059
c906108c
SS
3060 copy = (char *) alloca (p - *argptr + 1);
3061 memcpy (copy, *argptr, p - *argptr);
3062 copy[p - *argptr] = '\0';
3063 if (p != *argptr
3064 && copy[0]
c5aa993b 3065 && copy[0] == copy[p - *argptr - 1]
c906108c
SS
3066 && strchr (gdb_completer_quote_characters, copy[0]) != NULL)
3067 {
c5aa993b 3068 copy[p - *argptr - 1] = '\0';
c906108c
SS
3069 copy++;
3070 }
c5aa993b
JM
3071 while (*p == ' ' || *p == '\t')
3072 p++;
c906108c
SS
3073 *argptr = p;
3074
3075 /* If it starts with $: may be a legitimate variable or routine name
3076 (e.g. HP-UX millicode routines such as $$dyncall), or it may
c5aa993b 3077 be history value, or it may be a convenience variable */
c906108c
SS
3078
3079 if (*copy == '$')
3080 {
3081 value_ptr valx;
3082 int index = 0;
3083 int need_canonical = 0;
3084
3085 p = (copy[1] == '$') ? copy + 2 : copy + 1;
3086 while (*p >= '0' && *p <= '9')
c5aa993b
JM
3087 p++;
3088 if (!*p) /* reached end of token without hitting non-digit */
3089 {
3090 /* We have a value history reference */
3091 sscanf ((copy[1] == '$') ? copy + 2 : copy + 1, "%d", &index);
3092 valx = access_value_history ((copy[1] == '$') ? -index : index);
3093 if (TYPE_CODE (VALUE_TYPE (valx)) != TYPE_CODE_INT)
3094 error ("History values used in line specs must have integer values.");
3095 }
3096 else
3097 {
3098 /* Not all digits -- may be user variable/function or a
3099 convenience variable */
3100
3101 /* Look up entire name as a symbol first */
3102 sym = lookup_symbol (copy, 0, VAR_NAMESPACE, 0, &sym_symtab);
3103 s = (struct symtab *) 0;
3104 need_canonical = 1;
3105 /* Symbol was found --> jump to normal symbol processing.
3106 Code following "symbol_found" expects "copy" to have the
3107 symbol name, "sym" to have the symbol pointer, "s" to be
3108 a specified file's symtab, and sym_symtab to be the symbol's
3109 symtab. */
3110 if (sym)
3111 goto symbol_found;
3112
3113 /* If symbol was not found, look in minimal symbol tables */
3114 msymbol = lookup_minimal_symbol (copy, 0, 0);
3115 /* Min symbol was found --> jump to minsym processing. */
3116 if (msymbol)
3117 goto minimal_symbol_found;
3118
3119 /* Not a user variable or function -- must be convenience variable */
3120 need_canonical = (s == 0) ? 1 : 0;
3121 valx = value_of_internalvar (lookup_internalvar (copy + 1));
3122 if (TYPE_CODE (VALUE_TYPE (valx)) != TYPE_CODE_INT)
3123 error ("Convenience variables used in line specs must have integer values.");
3124 }
3125
3126 /* Either history value or convenience value from above, in valx */
c906108c
SS
3127 val.symtab = s ? s : default_symtab;
3128 val.line = value_as_long (valx);
3129 val.pc = 0;
3130
c5aa993b 3131 values.sals = (struct symtab_and_line *) xmalloc (sizeof val);
c906108c
SS
3132 values.sals[0] = val;
3133 values.nelts = 1;
3134
3135 if (need_canonical)
3136 build_canonical_line_spec (values.sals, NULL, canonical);
3137
3138 return values;
3139 }
3140
3141
3142 /* Look up that token as a variable.
3143 If file specified, use that file's per-file block to start with. */
3144
3145 sym = lookup_symbol (copy,
3146 (s ? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK)
3147 : get_selected_block ()),
3148 VAR_NAMESPACE, 0, &sym_symtab);
c5aa993b
JM
3149
3150symbol_found: /* We also jump here from inside the C++ class/namespace
3151 code on finding a symbol of the form "A::B::C" */
c906108c
SS
3152
3153 if (sym != NULL)
3154 {
3155 if (SYMBOL_CLASS (sym) == LOC_BLOCK)
3156 {
3157 /* Arg is the name of a function */
3158 values.sals = (struct symtab_and_line *)
3159 xmalloc (sizeof (struct symtab_and_line));
3160 values.sals[0] = find_function_start_sal (sym, funfirstline);
3161 values.nelts = 1;
3162
3163 /* Don't use the SYMBOL_LINE; if used at all it points to
3164 the line containing the parameters or thereabouts, not
3165 the first line of code. */
3166
3167 /* We might need a canonical line spec if it is a static
3168 function. */
3169 if (s == 0)
3170 {
3171 struct blockvector *bv = BLOCKVECTOR (sym_symtab);
3172 struct block *b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
3173 if (lookup_block_symbol (b, copy, VAR_NAMESPACE) != NULL)
3174 build_canonical_line_spec (values.sals, copy, canonical);
3175 }
3176 return values;
3177 }
3178 else
3179 {
3180 if (funfirstline)
3181 error ("\"%s\" is not a function", copy);
3182 else if (SYMBOL_LINE (sym) != 0)
3183 {
3184 /* We know its line number. */
3185 values.sals = (struct symtab_and_line *)
3186 xmalloc (sizeof (struct symtab_and_line));
3187 values.nelts = 1;
3188 memset (&values.sals[0], 0, sizeof (values.sals[0]));
3189 values.sals[0].symtab = sym_symtab;
3190 values.sals[0].line = SYMBOL_LINE (sym);
3191 return values;
3192 }
3193 else
3194 /* This can happen if it is compiled with a compiler which doesn't
3195 put out line numbers for variables. */
3196 /* FIXME: Shouldn't we just set .line and .symtab to zero
3197 and return? For example, "info line foo" could print
3198 the address. */
3199 error ("Line number not known for symbol \"%s\"", copy);
3200 }
3201 }
3202
3203 msymbol = lookup_minimal_symbol (copy, NULL, NULL);
3204
c5aa993b
JM
3205minimal_symbol_found: /* We also jump here from the case for variables
3206 that begin with '$' */
3207
c906108c
SS
3208 if (msymbol != NULL)
3209 {
3210 values.sals = (struct symtab_and_line *)
3211 xmalloc (sizeof (struct symtab_and_line));
c5aa993b
JM
3212 values.sals[0] = find_pc_sect_line (SYMBOL_VALUE_ADDRESS (msymbol),
3213 (struct sec *) 0, 0);
c906108c
SS
3214 values.sals[0].section = SYMBOL_BFD_SECTION (msymbol);
3215 if (funfirstline)
3216 {
3217 values.sals[0].pc += FUNCTION_START_OFFSET;
b83266a0 3218 values.sals[0].pc = SKIP_PROLOGUE (values.sals[0].pc);
c906108c
SS
3219 }
3220 values.nelts = 1;
3221 return values;
3222 }
3223
3224 if (!have_full_symbols () &&
3225 !have_partial_symbols () && !have_minimal_symbols ())
3226 error (no_symtab_msg);
3227
3228 error ("Function \"%s\" not defined.", copy);
c5aa993b 3229 return values; /* for lint */
c906108c
SS
3230}
3231
3232struct symtabs_and_lines
fba45db2 3233decode_line_spec (char *string, int funfirstline)
c906108c
SS
3234{
3235 struct symtabs_and_lines sals;
3236 if (string == 0)
3237 error ("Empty line specification.");
3238 sals = decode_line_1 (&string, funfirstline,
3239 current_source_symtab, current_source_line,
c5aa993b 3240 (char ***) NULL);
c906108c
SS
3241 if (*string)
3242 error ("Junk at end of line specification: %s", string);
3243 return sals;
3244}
3245
3246/* Given a list of NELTS symbols in SYM_ARR, return a list of lines to
3247 operate on (ask user if necessary).
3248 If CANONICAL is non-NULL return a corresponding array of mangled names
3249 as canonical line specs there. */
3250
3251static struct symtabs_and_lines
3252decode_line_2 (sym_arr, nelts, funfirstline, canonical)
3253 struct symbol *sym_arr[];
3254 int nelts;
3255 int funfirstline;
3256 char ***canonical;
3257{
3258 struct symtabs_and_lines values, return_values;
3259 char *args, *arg1;
3260 int i;
3261 char *prompt;
3262 char *symname;
3263 struct cleanup *old_chain;
c5aa993b 3264 char **canonical_arr = (char **) NULL;
c906108c 3265
c5aa993b
JM
3266 values.sals = (struct symtab_and_line *)
3267 alloca (nelts * sizeof (struct symtab_and_line));
3268 return_values.sals = (struct symtab_and_line *)
3269 xmalloc (nelts * sizeof (struct symtab_and_line));
c906108c
SS
3270 old_chain = make_cleanup (free, return_values.sals);
3271
3272 if (canonical)
3273 {
3274 canonical_arr = (char **) xmalloc (nelts * sizeof (char *));
3275 make_cleanup (free, canonical_arr);
3276 memset (canonical_arr, 0, nelts * sizeof (char *));
3277 *canonical = canonical_arr;
3278 }
3279
3280 i = 0;
c5aa993b 3281 printf_unfiltered ("[0] cancel\n[1] all\n");
c906108c
SS
3282 while (i < nelts)
3283 {
3284 INIT_SAL (&return_values.sals[i]); /* initialize to zeroes */
3285 INIT_SAL (&values.sals[i]);
3286 if (sym_arr[i] && SYMBOL_CLASS (sym_arr[i]) == LOC_BLOCK)
3287 {
3288 values.sals[i] = find_function_start_sal (sym_arr[i], funfirstline);
3289 printf_unfiltered ("[%d] %s at %s:%d\n",
c5aa993b 3290 (i + 2),
c906108c
SS
3291 SYMBOL_SOURCE_NAME (sym_arr[i]),
3292 values.sals[i].symtab->filename,
3293 values.sals[i].line);
3294 }
3295 else
3296 printf_unfiltered ("?HERE\n");
3297 i++;
3298 }
c5aa993b 3299
c906108c
SS
3300 if ((prompt = getenv ("PS2")) == NULL)
3301 {
3302 prompt = "> ";
3303 }
3304 args = command_line_input (prompt, 0, "overload-choice");
c5aa993b 3305
c906108c
SS
3306 if (args == 0 || *args == 0)
3307 error_no_arg ("one or more choice numbers");
3308
3309 i = 0;
3310 while (*args)
3311 {
3312 int num;
3313
3314 arg1 = args;
c5aa993b
JM
3315 while (*arg1 >= '0' && *arg1 <= '9')
3316 arg1++;
c906108c
SS
3317 if (*arg1 && *arg1 != ' ' && *arg1 != '\t')
3318 error ("Arguments must be choice numbers.");
3319
3320 num = atoi (args);
3321
3322 if (num == 0)
3323 error ("cancelled");
3324 else if (num == 1)
3325 {
3326 if (canonical_arr)
3327 {
3328 for (i = 0; i < nelts; i++)
3329 {
c5aa993b 3330 if (canonical_arr[i] == NULL)
c906108c
SS
3331 {
3332 symname = SYMBOL_NAME (sym_arr[i]);
c5aa993b 3333 canonical_arr[i] = savestring (symname, strlen (symname));
c906108c
SS
3334 }
3335 }
3336 }
3337 memcpy (return_values.sals, values.sals,
c5aa993b 3338 (nelts * sizeof (struct symtab_and_line)));
c906108c
SS
3339 return_values.nelts = nelts;
3340 discard_cleanups (old_chain);
3341 return return_values;
3342 }
3343
3344 if (num >= nelts + 2)
3345 {
3346 printf_unfiltered ("No choice number %d.\n", num);
3347 }
3348 else
3349 {
3350 num -= 2;
3351 if (values.sals[num].pc)
3352 {
3353 if (canonical_arr)
3354 {
3355 symname = SYMBOL_NAME (sym_arr[num]);
3356 make_cleanup (free, symname);
3357 canonical_arr[i] = savestring (symname, strlen (symname));
3358 }
3359 return_values.sals[i++] = values.sals[num];
3360 values.sals[num].pc = 0;
3361 }
3362 else
3363 {
3364 printf_unfiltered ("duplicate request for %d ignored.\n", num);
3365 }
3366 }
3367
3368 args = arg1;
c5aa993b
JM
3369 while (*args == ' ' || *args == '\t')
3370 args++;
c906108c
SS
3371 }
3372 return_values.nelts = i;
3373 discard_cleanups (old_chain);
3374 return return_values;
3375}
c906108c 3376\f
c5aa993b 3377
c906108c
SS
3378/* Slave routine for sources_info. Force line breaks at ,'s.
3379 NAME is the name to print and *FIRST is nonzero if this is the first
3380 name printed. Set *FIRST to zero. */
3381static void
fba45db2 3382output_source_filename (char *name, int *first)
c906108c
SS
3383{
3384 /* Table of files printed so far. Since a single source file can
3385 result in several partial symbol tables, we need to avoid printing
3386 it more than once. Note: if some of the psymtabs are read in and
3387 some are not, it gets printed both under "Source files for which
3388 symbols have been read" and "Source files for which symbols will
3389 be read in on demand". I consider this a reasonable way to deal
3390 with the situation. I'm not sure whether this can also happen for
3391 symtabs; it doesn't hurt to check. */
3392 static char **tab = NULL;
3393 /* Allocated size of tab in elements.
3394 Start with one 256-byte block (when using GNU malloc.c).
3395 24 is the malloc overhead when range checking is in effect. */
3396 static int tab_alloc_size = (256 - 24) / sizeof (char *);
3397 /* Current size of tab in elements. */
3398 static int tab_cur_size;
3399
3400 char **p;
3401
3402 if (*first)
3403 {
3404 if (tab == NULL)
3405 tab = (char **) xmalloc (tab_alloc_size * sizeof (*tab));
3406 tab_cur_size = 0;
3407 }
3408
3409 /* Is NAME in tab? */
3410 for (p = tab; p < tab + tab_cur_size; p++)
3411 if (STREQ (*p, name))
3412 /* Yes; don't print it again. */
3413 return;
3414 /* No; add it to tab. */
3415 if (tab_cur_size == tab_alloc_size)
3416 {
3417 tab_alloc_size *= 2;
3418 tab = (char **) xrealloc ((char *) tab, tab_alloc_size * sizeof (*tab));
3419 }
3420 tab[tab_cur_size++] = name;
3421
3422 if (*first)
3423 {
3424 *first = 0;
3425 }
3426 else
3427 {
3428 printf_filtered (", ");
3429 }
3430
3431 wrap_here ("");
3432 fputs_filtered (name, gdb_stdout);
c5aa993b 3433}
c906108c
SS
3434
3435static void
fba45db2 3436sources_info (char *ignore, int from_tty)
c906108c
SS
3437{
3438 register struct symtab *s;
3439 register struct partial_symtab *ps;
3440 register struct objfile *objfile;
3441 int first;
c5aa993b 3442
c906108c
SS
3443 if (!have_full_symbols () && !have_partial_symbols ())
3444 {
3445 error (no_symtab_msg);
3446 }
c5aa993b 3447
c906108c
SS
3448 printf_filtered ("Source files for which symbols have been read in:\n\n");
3449
3450 first = 1;
3451 ALL_SYMTABS (objfile, s)
c5aa993b
JM
3452 {
3453 output_source_filename (s->filename, &first);
3454 }
c906108c 3455 printf_filtered ("\n\n");
c5aa993b 3456
c906108c
SS
3457 printf_filtered ("Source files for which symbols will be read in on demand:\n\n");
3458
3459 first = 1;
3460 ALL_PSYMTABS (objfile, ps)
c5aa993b
JM
3461 {
3462 if (!ps->readin)
3463 {
3464 output_source_filename (ps->filename, &first);
3465 }
3466 }
c906108c
SS
3467 printf_filtered ("\n");
3468}
3469
3470static int
3471file_matches (file, files, nfiles)
3472 char *file;
3473 char *files[];
3474 int nfiles;
3475{
3476 int i;
3477
3478 if (file != NULL && nfiles != 0)
3479 {
3480 for (i = 0; i < nfiles; i++)
c5aa993b
JM
3481 {
3482 if (strcmp (files[i], basename (file)) == 0)
3483 return 1;
3484 }
c906108c
SS
3485 }
3486 else if (nfiles == 0)
3487 return 1;
3488 return 0;
3489}
3490
3491/* Free any memory associated with a search. */
3492void
fba45db2 3493free_search_symbols (struct symbol_search *symbols)
c906108c
SS
3494{
3495 struct symbol_search *p;
3496 struct symbol_search *next;
3497
3498 for (p = symbols; p != NULL; p = next)
3499 {
3500 next = p->next;
3501 free (p);
3502 }
3503}
3504
5bd98722
AC
3505static void
3506do_free_search_symbols_cleanup (void *symbols)
3507{
3508 free_search_symbols (symbols);
3509}
3510
3511struct cleanup *
3512make_cleanup_free_search_symbols (struct symbol_search *symbols)
3513{
3514 return make_cleanup (do_free_search_symbols_cleanup, symbols);
3515}
3516
3517
c906108c
SS
3518/* Search the symbol table for matches to the regular expression REGEXP,
3519 returning the results in *MATCHES.
3520
3521 Only symbols of KIND are searched:
c5aa993b
JM
3522 FUNCTIONS_NAMESPACE - search all functions
3523 TYPES_NAMESPACE - search all type names
3524 METHODS_NAMESPACE - search all methods NOT IMPLEMENTED
3525 VARIABLES_NAMESPACE - search all symbols, excluding functions, type names,
3526 and constants (enums)
c906108c
SS
3527
3528 free_search_symbols should be called when *MATCHES is no longer needed.
c5aa993b 3529 */
c906108c
SS
3530void
3531search_symbols (regexp, kind, nfiles, files, matches)
3532 char *regexp;
3533 namespace_enum kind;
3534 int nfiles;
3535 char *files[];
3536 struct symbol_search **matches;
c5aa993b 3537
c906108c
SS
3538{
3539 register struct symtab *s;
3540 register struct partial_symtab *ps;
3541 register struct blockvector *bv;
3542 struct blockvector *prev_bv = 0;
3543 register struct block *b;
3544 register int i = 0;
3545 register int j;
3546 register struct symbol *sym;
3547 struct partial_symbol **psym;
3548 struct objfile *objfile;
3549 struct minimal_symbol *msymbol;
3550 char *val;
3551 int found_misc = 0;
3552 static enum minimal_symbol_type types[]
c5aa993b
JM
3553 =
3554 {mst_data, mst_text, mst_abs, mst_unknown};
c906108c 3555 static enum minimal_symbol_type types2[]
c5aa993b
JM
3556 =
3557 {mst_bss, mst_file_text, mst_abs, mst_unknown};
c906108c 3558 static enum minimal_symbol_type types3[]
c5aa993b
JM
3559 =
3560 {mst_file_data, mst_solib_trampoline, mst_abs, mst_unknown};
c906108c 3561 static enum minimal_symbol_type types4[]
c5aa993b
JM
3562 =
3563 {mst_file_bss, mst_text, mst_abs, mst_unknown};
c906108c
SS
3564 enum minimal_symbol_type ourtype;
3565 enum minimal_symbol_type ourtype2;
3566 enum minimal_symbol_type ourtype3;
3567 enum minimal_symbol_type ourtype4;
3568 struct symbol_search *sr;
3569 struct symbol_search *psr;
3570 struct symbol_search *tail;
3571 struct cleanup *old_chain = NULL;
3572
3573 if (kind < LABEL_NAMESPACE)
3574 error ("must search on specific namespace");
3575
52204a0b
DT
3576 ourtype = types[(int) (kind - VARIABLES_NAMESPACE)];
3577 ourtype2 = types2[(int) (kind - VARIABLES_NAMESPACE)];
3578 ourtype3 = types3[(int) (kind - VARIABLES_NAMESPACE)];
3579 ourtype4 = types4[(int) (kind - VARIABLES_NAMESPACE)];
c906108c
SS
3580
3581 sr = *matches = NULL;
3582 tail = NULL;
3583
3584 if (regexp != NULL)
3585 {
3586 /* Make sure spacing is right for C++ operators.
3587 This is just a courtesy to make the matching less sensitive
3588 to how many spaces the user leaves between 'operator'
3589 and <TYPENAME> or <OPERATOR>. */
3590 char *opend;
3591 char *opname = operator_chars (regexp, &opend);
3592 if (*opname)
c5aa993b
JM
3593 {
3594 int fix = -1; /* -1 means ok; otherwise number of spaces needed. */
3595 if (isalpha (*opname) || *opname == '_' || *opname == '$')
3596 {
3597 /* There should 1 space between 'operator' and 'TYPENAME'. */
3598 if (opname[-1] != ' ' || opname[-2] == ' ')
3599 fix = 1;
3600 }
3601 else
3602 {
3603 /* There should 0 spaces between 'operator' and 'OPERATOR'. */
3604 if (opname[-1] == ' ')
3605 fix = 0;
3606 }
3607 /* If wrong number of spaces, fix it. */
3608 if (fix >= 0)
3609 {
3610 char *tmp = (char *) alloca (opend - opname + 10);
3611 sprintf (tmp, "operator%.*s%s", fix, " ", opname);
3612 regexp = tmp;
3613 }
3614 }
3615
c906108c 3616 if (0 != (val = re_comp (regexp)))
c5aa993b 3617 error ("Invalid regexp (%s): %s", val, regexp);
c906108c
SS
3618 }
3619
3620 /* Search through the partial symtabs *first* for all symbols
3621 matching the regexp. That way we don't have to reproduce all of
3622 the machinery below. */
3623
3624 ALL_PSYMTABS (objfile, ps)
c5aa993b
JM
3625 {
3626 struct partial_symbol **bound, **gbound, **sbound;
3627 int keep_going = 1;
3628
3629 if (ps->readin)
3630 continue;
3631
3632 gbound = objfile->global_psymbols.list + ps->globals_offset + ps->n_global_syms;
3633 sbound = objfile->static_psymbols.list + ps->statics_offset + ps->n_static_syms;
3634 bound = gbound;
3635
3636 /* Go through all of the symbols stored in a partial
3637 symtab in one loop. */
3638 psym = objfile->global_psymbols.list + ps->globals_offset;
3639 while (keep_going)
3640 {
3641 if (psym >= bound)
3642 {
3643 if (bound == gbound && ps->n_static_syms != 0)
3644 {
3645 psym = objfile->static_psymbols.list + ps->statics_offset;
3646 bound = sbound;
3647 }
3648 else
3649 keep_going = 0;
3650 continue;
3651 }
3652 else
3653 {
3654 QUIT;
3655
3656 /* If it would match (logic taken from loop below)
3657 load the file and go on to the next one */
3658 if (file_matches (ps->filename, files, nfiles)
3659 && ((regexp == NULL || SYMBOL_MATCHES_REGEXP (*psym))
3660 && ((kind == VARIABLES_NAMESPACE && SYMBOL_CLASS (*psym) != LOC_TYPEDEF
3661 && SYMBOL_CLASS (*psym) != LOC_BLOCK)
3662 || (kind == FUNCTIONS_NAMESPACE && SYMBOL_CLASS (*psym) == LOC_BLOCK)
3663 || (kind == TYPES_NAMESPACE && SYMBOL_CLASS (*psym) == LOC_TYPEDEF)
3664 || (kind == METHODS_NAMESPACE && SYMBOL_CLASS (*psym) == LOC_BLOCK))))
3665 {
3666 PSYMTAB_TO_SYMTAB (ps);
3667 keep_going = 0;
3668 }
3669 }
3670 psym++;
3671 }
3672 }
c906108c
SS
3673
3674 /* Here, we search through the minimal symbol tables for functions
3675 and variables that match, and force their symbols to be read.
3676 This is in particular necessary for demangled variable names,
3677 which are no longer put into the partial symbol tables.
3678 The symbol will then be found during the scan of symtabs below.
3679
3680 For functions, find_pc_symtab should succeed if we have debug info
3681 for the function, for variables we have to call lookup_symbol
3682 to determine if the variable has debug info.
3683 If the lookup fails, set found_misc so that we will rescan to print
3684 any matching symbols without debug info.
c5aa993b 3685 */
c906108c
SS
3686
3687 if (nfiles == 0 && (kind == VARIABLES_NAMESPACE || kind == FUNCTIONS_NAMESPACE))
3688 {
3689 ALL_MSYMBOLS (objfile, msymbol)
c5aa993b
JM
3690 {
3691 if (MSYMBOL_TYPE (msymbol) == ourtype ||
3692 MSYMBOL_TYPE (msymbol) == ourtype2 ||
3693 MSYMBOL_TYPE (msymbol) == ourtype3 ||
3694 MSYMBOL_TYPE (msymbol) == ourtype4)
3695 {
3696 if (regexp == NULL || SYMBOL_MATCHES_REGEXP (msymbol))
3697 {
3698 if (0 == find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol)))
3699 {
3700 if (kind == FUNCTIONS_NAMESPACE
3701 || lookup_symbol (SYMBOL_NAME (msymbol),
3702 (struct block *) NULL,
3703 VAR_NAMESPACE,
3704 0, (struct symtab **) NULL) == NULL)
3705 found_misc = 1;
3706 }
3707 }
3708 }
3709 }
c906108c
SS
3710 }
3711
3712 ALL_SYMTABS (objfile, s)
c5aa993b
JM
3713 {
3714 bv = BLOCKVECTOR (s);
3715 /* Often many files share a blockvector.
3716 Scan each blockvector only once so that
3717 we don't get every symbol many times.
3718 It happens that the first symtab in the list
3719 for any given blockvector is the main file. */
3720 if (bv != prev_bv)
3721 for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
3722 {
3723 b = BLOCKVECTOR_BLOCK (bv, i);
3724 /* Skip the sort if this block is always sorted. */
3725 if (!BLOCK_SHOULD_SORT (b))
3726 sort_block_syms (b);
3727 for (j = 0; j < BLOCK_NSYMS (b); j++)
3728 {
3729 QUIT;
3730 sym = BLOCK_SYM (b, j);
3731 if (file_matches (s->filename, files, nfiles)
3732 && ((regexp == NULL || SYMBOL_MATCHES_REGEXP (sym))
3733 && ((kind == VARIABLES_NAMESPACE && SYMBOL_CLASS (sym) != LOC_TYPEDEF
3734 && SYMBOL_CLASS (sym) != LOC_BLOCK
3735 && SYMBOL_CLASS (sym) != LOC_CONST)
3736 || (kind == FUNCTIONS_NAMESPACE && SYMBOL_CLASS (sym) == LOC_BLOCK)
3737 || (kind == TYPES_NAMESPACE && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
3738 || (kind == METHODS_NAMESPACE && SYMBOL_CLASS (sym) == LOC_BLOCK))))
3739 {
3740 /* match */
3741 psr = (struct symbol_search *) xmalloc (sizeof (struct symbol_search));
3742 psr->block = i;
3743 psr->symtab = s;
3744 psr->symbol = sym;
3745 psr->msymbol = NULL;
3746 psr->next = NULL;
3747 if (tail == NULL)
3748 {
3749 sr = psr;
5bd98722 3750 old_chain = make_cleanup_free_search_symbols (sr);
c5aa993b
JM
3751 }
3752 else
3753 tail->next = psr;
3754 tail = psr;
3755 }
3756 }
3757 }
3758 prev_bv = bv;
3759 }
c906108c
SS
3760
3761 /* If there are no eyes, avoid all contact. I mean, if there are
3762 no debug symbols, then print directly from the msymbol_vector. */
3763
3764 if (found_misc || kind != FUNCTIONS_NAMESPACE)
3765 {
3766 ALL_MSYMBOLS (objfile, msymbol)
c5aa993b
JM
3767 {
3768 if (MSYMBOL_TYPE (msymbol) == ourtype ||
3769 MSYMBOL_TYPE (msymbol) == ourtype2 ||
3770 MSYMBOL_TYPE (msymbol) == ourtype3 ||
3771 MSYMBOL_TYPE (msymbol) == ourtype4)
3772 {
3773 if (regexp == NULL || SYMBOL_MATCHES_REGEXP (msymbol))
3774 {
3775 /* Functions: Look up by address. */
3776 if (kind != FUNCTIONS_NAMESPACE ||
3777 (0 == find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol))))
3778 {
3779 /* Variables/Absolutes: Look up by name */
3780 if (lookup_symbol (SYMBOL_NAME (msymbol),
3781 (struct block *) NULL, VAR_NAMESPACE,
3782 0, (struct symtab **) NULL) == NULL)
3783 {
3784 /* match */
3785 psr = (struct symbol_search *) xmalloc (sizeof (struct symbol_search));
3786 psr->block = i;
3787 psr->msymbol = msymbol;
3788 psr->symtab = NULL;
3789 psr->symbol = NULL;
3790 psr->next = NULL;
3791 if (tail == NULL)
3792 {
3793 sr = psr;
5bd98722 3794 old_chain = make_cleanup_free_search_symbols (sr);
c5aa993b
JM
3795 }
3796 else
3797 tail->next = psr;
3798 tail = psr;
3799 }
3800 }
3801 }
3802 }
3803 }
c906108c
SS
3804 }
3805
3806 *matches = sr;
3807 if (sr != NULL)
3808 discard_cleanups (old_chain);
3809}
3810
3811/* Helper function for symtab_symbol_info, this function uses
3812 the data returned from search_symbols() to print information
3813 regarding the match to gdb_stdout.
c5aa993b 3814 */
c906108c 3815static void
fba45db2
KB
3816print_symbol_info (namespace_enum kind, struct symtab *s, struct symbol *sym,
3817 int block, char *last)
c906108c
SS
3818{
3819 if (last == NULL || strcmp (last, s->filename) != 0)
3820 {
3821 fputs_filtered ("\nFile ", gdb_stdout);
3822 fputs_filtered (s->filename, gdb_stdout);
3823 fputs_filtered (":\n", gdb_stdout);
3824 }
3825
3826 if (kind != TYPES_NAMESPACE && block == STATIC_BLOCK)
3827 printf_filtered ("static ");
c5aa993b 3828
c906108c
SS
3829 /* Typedef that is not a C++ class */
3830 if (kind == TYPES_NAMESPACE
3831 && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE)
c5aa993b 3832 c_typedef_print (SYMBOL_TYPE (sym), sym, gdb_stdout);
c906108c 3833 /* variable, func, or typedef-that-is-c++-class */
c5aa993b
JM
3834 else if (kind < TYPES_NAMESPACE ||
3835 (kind == TYPES_NAMESPACE &&
3836 SYMBOL_NAMESPACE (sym) == STRUCT_NAMESPACE))
c906108c
SS
3837 {
3838 type_print (SYMBOL_TYPE (sym),
c5aa993b
JM
3839 (SYMBOL_CLASS (sym) == LOC_TYPEDEF
3840 ? "" : SYMBOL_SOURCE_NAME (sym)),
3841 gdb_stdout, 0);
c906108c
SS
3842
3843 printf_filtered (";\n");
3844 }
3845 else
3846 {
c5aa993b 3847#if 0
c906108c
SS
3848 /* Tiemann says: "info methods was never implemented." */
3849 char *demangled_name;
c5aa993b
JM
3850 c_type_print_base (TYPE_FN_FIELD_TYPE (t, block),
3851 gdb_stdout, 0, 0);
3852 c_type_print_varspec_prefix (TYPE_FN_FIELD_TYPE (t, block),
3853 gdb_stdout, 0);
c906108c 3854 if (TYPE_FN_FIELD_STUB (t, block))
c5aa993b 3855 check_stub_method (TYPE_DOMAIN_TYPE (type), j, block);
c906108c 3856 demangled_name =
c5aa993b
JM
3857 cplus_demangle (TYPE_FN_FIELD_PHYSNAME (t, block),
3858 DMGL_ANSI | DMGL_PARAMS);
c906108c 3859 if (demangled_name == NULL)
c5aa993b
JM
3860 fprintf_filtered (stream, "<badly mangled name %s>",
3861 TYPE_FN_FIELD_PHYSNAME (t, block));
c906108c 3862 else
c5aa993b
JM
3863 {
3864 fputs_filtered (demangled_name, stream);
3865 free (demangled_name);
3866 }
3867#endif
c906108c
SS
3868 }
3869}
3870
3871/* This help function for symtab_symbol_info() prints information
3872 for non-debugging symbols to gdb_stdout.
c5aa993b 3873 */
c906108c 3874static void
fba45db2 3875print_msymbol_info (struct minimal_symbol *msymbol)
c906108c
SS
3876{
3877 printf_filtered (" %08lx %s\n",
c5aa993b
JM
3878 (unsigned long) SYMBOL_VALUE_ADDRESS (msymbol),
3879 SYMBOL_SOURCE_NAME (msymbol));
c906108c
SS
3880}
3881
3882/* This is the guts of the commands "info functions", "info types", and
3883 "info variables". It calls search_symbols to find all matches and then
3884 print_[m]symbol_info to print out some useful information about the
3885 matches.
c5aa993b 3886 */
c906108c 3887static void
fba45db2 3888symtab_symbol_info (char *regexp, namespace_enum kind, int from_tty)
c906108c
SS
3889{
3890 static char *classnames[]
c5aa993b
JM
3891 =
3892 {"variable", "function", "type", "method"};
c906108c
SS
3893 struct symbol_search *symbols;
3894 struct symbol_search *p;
3895 struct cleanup *old_chain;
3896 char *last_filename = NULL;
3897 int first = 1;
3898
3899 /* must make sure that if we're interrupted, symbols gets freed */
3900 search_symbols (regexp, kind, 0, (char **) NULL, &symbols);
5bd98722 3901 old_chain = make_cleanup_free_search_symbols (symbols);
c906108c
SS
3902
3903 printf_filtered (regexp
c5aa993b
JM
3904 ? "All %ss matching regular expression \"%s\":\n"
3905 : "All defined %ss:\n",
52204a0b 3906 classnames[(int) (kind - VARIABLES_NAMESPACE)], regexp);
c906108c
SS
3907
3908 for (p = symbols; p != NULL; p = p->next)
3909 {
3910 QUIT;
3911
3912 if (p->msymbol != NULL)
c5aa993b
JM
3913 {
3914 if (first)
3915 {
3916 printf_filtered ("\nNon-debugging symbols:\n");
3917 first = 0;
3918 }
3919 print_msymbol_info (p->msymbol);
3920 }
c906108c 3921 else
c5aa993b
JM
3922 {
3923 print_symbol_info (kind,
3924 p->symtab,
3925 p->symbol,
3926 p->block,
3927 last_filename);
3928 last_filename = p->symtab->filename;
3929 }
c906108c
SS
3930 }
3931
3932 do_cleanups (old_chain);
3933}
3934
3935static void
fba45db2 3936variables_info (char *regexp, int from_tty)
c906108c
SS
3937{
3938 symtab_symbol_info (regexp, VARIABLES_NAMESPACE, from_tty);
3939}
3940
3941static void
fba45db2 3942functions_info (char *regexp, int from_tty)
c906108c
SS
3943{
3944 symtab_symbol_info (regexp, FUNCTIONS_NAMESPACE, from_tty);
3945}
3946
357e46e7 3947
c906108c 3948static void
fba45db2 3949types_info (char *regexp, int from_tty)
c906108c
SS
3950{
3951 symtab_symbol_info (regexp, TYPES_NAMESPACE, from_tty);
3952}
3953
3954#if 0
3955/* Tiemann says: "info methods was never implemented." */
3956static void
fba45db2 3957methods_info (char *regexp)
c906108c
SS
3958{
3959 symtab_symbol_info (regexp, METHODS_NAMESPACE, 0, from_tty);
3960}
3961#endif /* 0 */
3962
3963/* Breakpoint all functions matching regular expression. */
8b93c638
JM
3964#ifdef UI_OUT
3965void
fba45db2 3966rbreak_command_wrapper (char *regexp, int from_tty)
8b93c638
JM
3967{
3968 rbreak_command (regexp, from_tty);
3969}
3970#endif
c906108c 3971static void
fba45db2 3972rbreak_command (char *regexp, int from_tty)
c906108c
SS
3973{
3974 struct symbol_search *ss;
3975 struct symbol_search *p;
3976 struct cleanup *old_chain;
3977
3978 search_symbols (regexp, FUNCTIONS_NAMESPACE, 0, (char **) NULL, &ss);
5bd98722 3979 old_chain = make_cleanup_free_search_symbols (ss);
c906108c
SS
3980
3981 for (p = ss; p != NULL; p = p->next)
3982 {
3983 if (p->msymbol == NULL)
c5aa993b
JM
3984 {
3985 char *string = (char *) alloca (strlen (p->symtab->filename)
3986 + strlen (SYMBOL_NAME (p->symbol))
3987 + 4);
3988 strcpy (string, p->symtab->filename);
3989 strcat (string, ":'");
3990 strcat (string, SYMBOL_NAME (p->symbol));
3991 strcat (string, "'");
3992 break_command (string, from_tty);
3993 print_symbol_info (FUNCTIONS_NAMESPACE,
3994 p->symtab,
3995 p->symbol,
3996 p->block,
3997 p->symtab->filename);
3998 }
c906108c 3999 else
c5aa993b
JM
4000 {
4001 break_command (SYMBOL_NAME (p->msymbol), from_tty);
4002 printf_filtered ("<function, no debug info> %s;\n",
4003 SYMBOL_SOURCE_NAME (p->msymbol));
4004 }
c906108c
SS
4005 }
4006
4007 do_cleanups (old_chain);
4008}
c906108c 4009\f
c5aa993b 4010
c906108c
SS
4011/* Return Nonzero if block a is lexically nested within block b,
4012 or if a and b have the same pc range.
4013 Return zero otherwise. */
4014int
fba45db2 4015contained_in (struct block *a, struct block *b)
c906108c
SS
4016{
4017 if (!a || !b)
4018 return 0;
4019 return BLOCK_START (a) >= BLOCK_START (b)
c5aa993b 4020 && BLOCK_END (a) <= BLOCK_END (b);
c906108c 4021}
c906108c 4022\f
c5aa993b 4023
c906108c
SS
4024/* Helper routine for make_symbol_completion_list. */
4025
4026static int return_val_size;
4027static int return_val_index;
4028static char **return_val;
4029
4030#define COMPLETION_LIST_ADD_SYMBOL(symbol, sym_text, len, text, word) \
4031 do { \
4032 if (SYMBOL_DEMANGLED_NAME (symbol) != NULL) \
4033 /* Put only the mangled name on the list. */ \
4034 /* Advantage: "b foo<TAB>" completes to "b foo(int, int)" */ \
4035 /* Disadvantage: "b foo__i<TAB>" doesn't complete. */ \
4036 completion_list_add_name \
4037 (SYMBOL_DEMANGLED_NAME (symbol), (sym_text), (len), (text), (word)); \
4038 else \
4039 completion_list_add_name \
4040 (SYMBOL_NAME (symbol), (sym_text), (len), (text), (word)); \
4041 } while (0)
4042
4043/* Test to see if the symbol specified by SYMNAME (which is already
c5aa993b
JM
4044 demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN
4045 characters. If so, add it to the current completion list. */
c906108c
SS
4046
4047static void
fba45db2
KB
4048completion_list_add_name (char *symname, char *sym_text, int sym_text_len,
4049 char *text, char *word)
c906108c
SS
4050{
4051 int newsize;
4052 int i;
4053
4054 /* clip symbols that cannot match */
4055
4056 if (strncmp (symname, sym_text, sym_text_len) != 0)
4057 {
4058 return;
4059 }
4060
4061 /* Clip any symbol names that we've already considered. (This is a
4062 time optimization) */
4063
4064 for (i = 0; i < return_val_index; ++i)
4065 {
4066 if (STREQ (symname, return_val[i]))
4067 {
4068 return;
4069 }
4070 }
c5aa993b 4071
c906108c
SS
4072 /* We have a match for a completion, so add SYMNAME to the current list
4073 of matches. Note that the name is moved to freshly malloc'd space. */
4074
4075 {
4076 char *new;
4077 if (word == sym_text)
4078 {
4079 new = xmalloc (strlen (symname) + 5);
4080 strcpy (new, symname);
4081 }
4082 else if (word > sym_text)
4083 {
4084 /* Return some portion of symname. */
4085 new = xmalloc (strlen (symname) + 5);
4086 strcpy (new, symname + (word - sym_text));
4087 }
4088 else
4089 {
4090 /* Return some of SYM_TEXT plus symname. */
4091 new = xmalloc (strlen (symname) + (sym_text - word) + 5);
4092 strncpy (new, word, sym_text - word);
4093 new[sym_text - word] = '\0';
4094 strcat (new, symname);
4095 }
4096
4097 /* Recheck for duplicates if we intend to add a modified symbol. */
4098 if (word != sym_text)
4099 {
4100 for (i = 0; i < return_val_index; ++i)
4101 {
4102 if (STREQ (new, return_val[i]))
4103 {
4104 free (new);
4105 return;
4106 }
4107 }
4108 }
4109
4110 if (return_val_index + 3 > return_val_size)
4111 {
4112 newsize = (return_val_size *= 2) * sizeof (char *);
4113 return_val = (char **) xrealloc ((char *) return_val, newsize);
4114 }
4115 return_val[return_val_index++] = new;
4116 return_val[return_val_index] = NULL;
4117 }
4118}
4119
4120/* Return a NULL terminated array of all symbols (regardless of class) which
4121 begin by matching TEXT. If the answer is no symbols, then the return value
4122 is an array which contains only a NULL pointer.
4123
4124 Problem: All of the symbols have to be copied because readline frees them.
4125 I'm not going to worry about this; hopefully there won't be that many. */
4126
4127char **
fba45db2 4128make_symbol_completion_list (char *text, char *word)
c906108c
SS
4129{
4130 register struct symbol *sym;
4131 register struct symtab *s;
4132 register struct partial_symtab *ps;
4133 register struct minimal_symbol *msymbol;
4134 register struct objfile *objfile;
4135 register struct block *b, *surrounding_static_block = 0;
4136 register int i, j;
4137 struct partial_symbol **psym;
4138 /* The symbol we are completing on. Points in same buffer as text. */
4139 char *sym_text;
4140 /* Length of sym_text. */
4141 int sym_text_len;
4142
4143 /* Now look for the symbol we are supposed to complete on.
4144 FIXME: This should be language-specific. */
4145 {
4146 char *p;
4147 char quote_found;
4148 char *quote_pos = NULL;
4149
4150 /* First see if this is a quoted string. */
4151 quote_found = '\0';
4152 for (p = text; *p != '\0'; ++p)
4153 {
4154 if (quote_found != '\0')
4155 {
4156 if (*p == quote_found)
4157 /* Found close quote. */
4158 quote_found = '\0';
4159 else if (*p == '\\' && p[1] == quote_found)
4160 /* A backslash followed by the quote character
c5aa993b 4161 doesn't end the string. */
c906108c
SS
4162 ++p;
4163 }
4164 else if (*p == '\'' || *p == '"')
4165 {
4166 quote_found = *p;
4167 quote_pos = p;
4168 }
4169 }
4170 if (quote_found == '\'')
4171 /* A string within single quotes can be a symbol, so complete on it. */
4172 sym_text = quote_pos + 1;
4173 else if (quote_found == '"')
4174 /* A double-quoted string is never a symbol, nor does it make sense
c5aa993b 4175 to complete it any other way. */
c906108c
SS
4176 return NULL;
4177 else
4178 {
4179 /* It is not a quoted string. Break it based on the characters
4180 which are in symbols. */
4181 while (p > text)
4182 {
4183 if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0')
4184 --p;
4185 else
4186 break;
4187 }
4188 sym_text = p;
4189 }
4190 }
4191
4192 sym_text_len = strlen (sym_text);
4193
4194 return_val_size = 100;
4195 return_val_index = 0;
4196 return_val = (char **) xmalloc ((return_val_size + 1) * sizeof (char *));
4197 return_val[0] = NULL;
4198
4199 /* Look through the partial symtabs for all symbols which begin
4200 by matching SYM_TEXT. Add each one that you find to the list. */
4201
4202 ALL_PSYMTABS (objfile, ps)
c5aa993b
JM
4203 {
4204 /* If the psymtab's been read in we'll get it when we search
4205 through the blockvector. */
4206 if (ps->readin)
4207 continue;
4208
4209 for (psym = objfile->global_psymbols.list + ps->globals_offset;
4210 psym < (objfile->global_psymbols.list + ps->globals_offset
4211 + ps->n_global_syms);
4212 psym++)
4213 {
4214 /* If interrupted, then quit. */
4215 QUIT;
4216 COMPLETION_LIST_ADD_SYMBOL (*psym, sym_text, sym_text_len, text, word);
4217 }
4218
4219 for (psym = objfile->static_psymbols.list + ps->statics_offset;
4220 psym < (objfile->static_psymbols.list + ps->statics_offset
4221 + ps->n_static_syms);
4222 psym++)
4223 {
4224 QUIT;
4225 COMPLETION_LIST_ADD_SYMBOL (*psym, sym_text, sym_text_len, text, word);
4226 }
4227 }
c906108c
SS
4228
4229 /* At this point scan through the misc symbol vectors and add each
4230 symbol you find to the list. Eventually we want to ignore
4231 anything that isn't a text symbol (everything else will be
4232 handled by the psymtab code above). */
4233
4234 ALL_MSYMBOLS (objfile, msymbol)
c5aa993b
JM
4235 {
4236 QUIT;
4237 COMPLETION_LIST_ADD_SYMBOL (msymbol, sym_text, sym_text_len, text, word);
4238 }
c906108c
SS
4239
4240 /* Search upwards from currently selected frame (so that we can
4241 complete on local vars. */
4242
4243 for (b = get_selected_block (); b != NULL; b = BLOCK_SUPERBLOCK (b))
4244 {
4245 if (!BLOCK_SUPERBLOCK (b))
4246 {
c5aa993b 4247 surrounding_static_block = b; /* For elmin of dups */
c906108c 4248 }
c5aa993b 4249
c906108c 4250 /* Also catch fields of types defined in this places which match our
c5aa993b 4251 text string. Only complete on types visible from current context. */
c906108c
SS
4252
4253 for (i = 0; i < BLOCK_NSYMS (b); i++)
4254 {
4255 sym = BLOCK_SYM (b, i);
4256 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
4257 if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
4258 {
4259 struct type *t = SYMBOL_TYPE (sym);
4260 enum type_code c = TYPE_CODE (t);
4261
4262 if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
4263 {
4264 for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++)
4265 {
4266 if (TYPE_FIELD_NAME (t, j))
4267 {
4268 completion_list_add_name (TYPE_FIELD_NAME (t, j),
c5aa993b 4269 sym_text, sym_text_len, text, word);
c906108c
SS
4270 }
4271 }
4272 }
4273 }
4274 }
4275 }
4276
4277 /* Go through the symtabs and check the externs and statics for
4278 symbols which match. */
4279
4280 ALL_SYMTABS (objfile, s)
c5aa993b
JM
4281 {
4282 QUIT;
4283 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
4284 for (i = 0; i < BLOCK_NSYMS (b); i++)
4285 {
4286 sym = BLOCK_SYM (b, i);
4287 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
4288 }
4289 }
c906108c
SS
4290
4291 ALL_SYMTABS (objfile, s)
c5aa993b
JM
4292 {
4293 QUIT;
4294 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
4295 /* Don't do this block twice. */
4296 if (b == surrounding_static_block)
4297 continue;
4298 for (i = 0; i < BLOCK_NSYMS (b); i++)
4299 {
4300 sym = BLOCK_SYM (b, i);
4301 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
4302 }
4303 }
c906108c
SS
4304
4305 return (return_val);
4306}
4307
4308/* Determine if PC is in the prologue of a function. The prologue is the area
4309 between the first instruction of a function, and the first executable line.
4310 Returns 1 if PC *might* be in prologue, 0 if definately *not* in prologue.
4311
4312 If non-zero, func_start is where we think the prologue starts, possibly
4313 by previous examination of symbol table information.
4314 */
4315
4316int
fba45db2 4317in_prologue (CORE_ADDR pc, CORE_ADDR func_start)
c906108c
SS
4318{
4319 struct symtab_and_line sal;
4320 CORE_ADDR func_addr, func_end;
4321
54cf9c03
EZ
4322 /* We have several sources of information we can consult to figure
4323 this out.
4324 - Compilers usually emit line number info that marks the prologue
4325 as its own "source line". So the ending address of that "line"
4326 is the end of the prologue. If available, this is the most
4327 reliable method.
4328 - The minimal symbols and partial symbols, which can usually tell
4329 us the starting and ending addresses of a function.
4330 - If we know the function's start address, we can call the
4331 architecture-defined SKIP_PROLOGUE function to analyze the
4332 instruction stream and guess where the prologue ends.
4333 - Our `func_start' argument; if non-zero, this is the caller's
4334 best guess as to the function's entry point. At the time of
4335 this writing, handle_inferior_event doesn't get this right, so
4336 it should be our last resort. */
4337
4338 /* Consult the partial symbol table, to find which function
4339 the PC is in. */
4340 if (! find_pc_partial_function (pc, NULL, &func_addr, &func_end))
4341 {
4342 CORE_ADDR prologue_end;
c906108c 4343
54cf9c03
EZ
4344 /* We don't even have minsym information, so fall back to using
4345 func_start, if given. */
4346 if (! func_start)
4347 return 1; /* We *might* be in a prologue. */
c906108c 4348
54cf9c03 4349 prologue_end = SKIP_PROLOGUE (func_start);
c906108c 4350
54cf9c03
EZ
4351 return func_start <= pc && pc < prologue_end;
4352 }
c906108c 4353
54cf9c03
EZ
4354 /* If we have line number information for the function, that's
4355 usually pretty reliable. */
4356 sal = find_pc_line (func_addr, 0);
c906108c 4357
54cf9c03
EZ
4358 /* Now sal describes the source line at the function's entry point,
4359 which (by convention) is the prologue. The end of that "line",
4360 sal.end, is the end of the prologue.
4361
4362 Note that, for functions whose source code is all on a single
4363 line, the line number information doesn't always end up this way.
4364 So we must verify that our purported end-of-prologue address is
4365 *within* the function, not at its start or end. */
4366 if (sal.line == 0
4367 || sal.end <= func_addr
4368 || func_end <= sal.end)
4369 {
4370 /* We don't have any good line number info, so use the minsym
4371 information, together with the architecture-specific prologue
4372 scanning code. */
4373 CORE_ADDR prologue_end = SKIP_PROLOGUE (func_addr);
c906108c 4374
54cf9c03
EZ
4375 return func_addr <= pc && pc < prologue_end;
4376 }
c906108c 4377
54cf9c03
EZ
4378 /* We have line number info, and it looks good. */
4379 return func_addr <= pc && pc < sal.end;
c906108c
SS
4380}
4381
4382
4383/* Begin overload resolution functions */
4384/* Helper routine for make_symbol_completion_list. */
4385
4386static int sym_return_val_size;
4387static int sym_return_val_index;
4388static struct symbol **sym_return_val;
4389
4390/* Test to see if the symbol specified by SYMNAME (which is already
c5aa993b
JM
4391 demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN
4392 characters. If so, add it to the current completion list. */
c906108c
SS
4393
4394static void
fba45db2 4395overload_list_add_symbol (struct symbol *sym, char *oload_name)
c906108c
SS
4396{
4397 int newsize;
4398 int i;
4399
4400 /* Get the demangled name without parameters */
c5aa993b 4401 char *sym_name = cplus_demangle (SYMBOL_NAME (sym), DMGL_ARM | DMGL_ANSI);
c906108c
SS
4402 if (!sym_name)
4403 {
4404 sym_name = (char *) xmalloc (strlen (SYMBOL_NAME (sym)) + 1);
4405 strcpy (sym_name, SYMBOL_NAME (sym));
4406 }
4407
4408 /* skip symbols that cannot match */
4409 if (strcmp (sym_name, oload_name) != 0)
917317f4
JM
4410 {
4411 free (sym_name);
4412 return;
4413 }
c906108c
SS
4414
4415 /* If there is no type information, we can't do anything, so skip */
4416 if (SYMBOL_TYPE (sym) == NULL)
4417 return;
4418
4419 /* skip any symbols that we've already considered. */
4420 for (i = 0; i < sym_return_val_index; ++i)
4421 if (!strcmp (SYMBOL_NAME (sym), SYMBOL_NAME (sym_return_val[i])))
4422 return;
4423
4424 /* We have a match for an overload instance, so add SYM to the current list
4425 * of overload instances */
4426 if (sym_return_val_index + 3 > sym_return_val_size)
4427 {
4428 newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
4429 sym_return_val = (struct symbol **) xrealloc ((char *) sym_return_val, newsize);
4430 }
4431 sym_return_val[sym_return_val_index++] = sym;
4432 sym_return_val[sym_return_val_index] = NULL;
c5aa993b 4433
c906108c
SS
4434 free (sym_name);
4435}
4436
4437/* Return a null-terminated list of pointers to function symbols that
4438 * match name of the supplied symbol FSYM.
4439 * This is used in finding all overloaded instances of a function name.
4440 * This has been modified from make_symbol_completion_list. */
4441
4442
4443struct symbol **
fba45db2 4444make_symbol_overload_list (struct symbol *fsym)
c906108c
SS
4445{
4446 register struct symbol *sym;
4447 register struct symtab *s;
4448 register struct partial_symtab *ps;
c906108c
SS
4449 register struct objfile *objfile;
4450 register struct block *b, *surrounding_static_block = 0;
d4f3574e 4451 register int i;
c906108c
SS
4452 /* The name we are completing on. */
4453 char *oload_name = NULL;
4454 /* Length of name. */
4455 int oload_name_len = 0;
4456
4457 /* Look for the symbol we are supposed to complete on.
4458 * FIXME: This should be language-specific. */
4459
4460 oload_name = cplus_demangle (SYMBOL_NAME (fsym), DMGL_ARM | DMGL_ANSI);
4461 if (!oload_name)
4462 {
4463 oload_name = (char *) xmalloc (strlen (SYMBOL_NAME (fsym)) + 1);
4464 strcpy (oload_name, SYMBOL_NAME (fsym));
4465 }
4466 oload_name_len = strlen (oload_name);
4467
4468 sym_return_val_size = 100;
4469 sym_return_val_index = 0;
4470 sym_return_val = (struct symbol **) xmalloc ((sym_return_val_size + 1) * sizeof (struct symbol *));
4471 sym_return_val[0] = NULL;
4472
4473 /* Look through the partial symtabs for all symbols which begin
917317f4 4474 by matching OLOAD_NAME. Make sure we read that symbol table in. */
c906108c
SS
4475
4476 ALL_PSYMTABS (objfile, ps)
c5aa993b 4477 {
d4f3574e
SS
4478 struct partial_symbol **psym;
4479
c5aa993b
JM
4480 /* If the psymtab's been read in we'll get it when we search
4481 through the blockvector. */
4482 if (ps->readin)
4483 continue;
4484
4485 for (psym = objfile->global_psymbols.list + ps->globals_offset;
4486 psym < (objfile->global_psymbols.list + ps->globals_offset
4487 + ps->n_global_syms);
4488 psym++)
4489 {
4490 /* If interrupted, then quit. */
4491 QUIT;
917317f4
JM
4492 /* This will cause the symbol table to be read if it has not yet been */
4493 s = PSYMTAB_TO_SYMTAB (ps);
c5aa993b
JM
4494 }
4495
4496 for (psym = objfile->static_psymbols.list + ps->statics_offset;
4497 psym < (objfile->static_psymbols.list + ps->statics_offset
4498 + ps->n_static_syms);
4499 psym++)
4500 {
4501 QUIT;
917317f4
JM
4502 /* This will cause the symbol table to be read if it has not yet been */
4503 s = PSYMTAB_TO_SYMTAB (ps);
c5aa993b
JM
4504 }
4505 }
c906108c 4506
c906108c
SS
4507 /* Search upwards from currently selected frame (so that we can
4508 complete on local vars. */
4509
4510 for (b = get_selected_block (); b != NULL; b = BLOCK_SUPERBLOCK (b))
4511 {
4512 if (!BLOCK_SUPERBLOCK (b))
4513 {
c5aa993b 4514 surrounding_static_block = b; /* For elimination of dups */
c906108c 4515 }
c5aa993b 4516
c906108c 4517 /* Also catch fields of types defined in this places which match our
c5aa993b 4518 text string. Only complete on types visible from current context. */
c906108c
SS
4519
4520 for (i = 0; i < BLOCK_NSYMS (b); i++)
4521 {
4522 sym = BLOCK_SYM (b, i);
4523 overload_list_add_symbol (sym, oload_name);
4524 }
4525 }
4526
4527 /* Go through the symtabs and check the externs and statics for
4528 symbols which match. */
4529
4530 ALL_SYMTABS (objfile, s)
c5aa993b
JM
4531 {
4532 QUIT;
4533 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
4534 for (i = 0; i < BLOCK_NSYMS (b); i++)
4535 {
4536 sym = BLOCK_SYM (b, i);
4537 overload_list_add_symbol (sym, oload_name);
4538 }
4539 }
c906108c
SS
4540
4541 ALL_SYMTABS (objfile, s)
c5aa993b
JM
4542 {
4543 QUIT;
4544 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
4545 /* Don't do this block twice. */
4546 if (b == surrounding_static_block)
4547 continue;
4548 for (i = 0; i < BLOCK_NSYMS (b); i++)
4549 {
4550 sym = BLOCK_SYM (b, i);
4551 overload_list_add_symbol (sym, oload_name);
4552 }
4553 }
c906108c
SS
4554
4555 free (oload_name);
4556
4557 return (sym_return_val);
4558}
4559
4560/* End of overload resolution functions */
c906108c 4561\f
c5aa993b 4562
c906108c 4563void
fba45db2 4564_initialize_symtab (void)
c906108c
SS
4565{
4566 add_info ("variables", variables_info,
c5aa993b 4567 "All global and static variable names, or those matching REGEXP.");
c906108c 4568 if (dbx_commands)
c5aa993b
JM
4569 add_com ("whereis", class_info, variables_info,
4570 "All global and static variable names, or those matching REGEXP.");
c906108c
SS
4571
4572 add_info ("functions", functions_info,
4573 "All function names, or those matching REGEXP.");
4574
357e46e7 4575
c906108c
SS
4576 /* FIXME: This command has at least the following problems:
4577 1. It prints builtin types (in a very strange and confusing fashion).
4578 2. It doesn't print right, e.g. with
c5aa993b
JM
4579 typedef struct foo *FOO
4580 type_print prints "FOO" when we want to make it (in this situation)
4581 print "struct foo *".
c906108c
SS
4582 I also think "ptype" or "whatis" is more likely to be useful (but if
4583 there is much disagreement "info types" can be fixed). */
4584 add_info ("types", types_info,
4585 "All type names, or those matching REGEXP.");
4586
4587#if 0
4588 add_info ("methods", methods_info,
4589 "All method names, or those matching REGEXP::REGEXP.\n\
4590If the class qualifier is omitted, it is assumed to be the current scope.\n\
4591If the first REGEXP is omitted, then all methods matching the second REGEXP\n\
4592are listed.");
4593#endif
4594 add_info ("sources", sources_info,
4595 "Source files in the program.");
4596
4597 add_com ("rbreak", class_breakpoint, rbreak_command,
c5aa993b 4598 "Set a breakpoint for all functions matching REGEXP.");
c906108c
SS
4599
4600 if (xdb_commands)
4601 {
4602 add_com ("lf", class_info, sources_info, "Source files in the program");
4603 add_com ("lg", class_info, variables_info,
c5aa993b 4604 "All global and static variable names, or those matching REGEXP.");
c906108c
SS
4605 }
4606
4607 /* Initialize the one built-in type that isn't language dependent... */
4608 builtin_type_error = init_type (TYPE_CODE_ERROR, 0, 0,
4609 "<unknown type>", (struct objfile *) NULL);
4610}
This page took 0.275284 seconds and 4 git commands to generate.