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