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