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