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