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