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