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