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