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