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