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