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