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