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