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