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