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