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