Only make a nullterminated string if we need to
[deliverable/binutils-gdb.git] / gdb / symtab.c
CommitLineData
c906108c 1/* Symbol table lookup for the GNU debugger, GDB.
8926118c 2
42a4f53d 3 Copyright (C) 1986-2019 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"
529480d0 43#include "cli/cli-utils.h"
1ed9f74e 44#include "cli/cli-style.h"
cce0e923 45#include "fnmatch.h"
2de7ced7 46#include "hashtab.h"
12615cba 47#include "typeprint.h"
2de7ced7 48
04ea0df1 49#include "gdb_obstack.h"
fe898f56 50#include "block.h"
de4f826b 51#include "dictionary.h"
c906108c
SS
52
53#include <sys/types.h>
54#include <fcntl.h>
53ce3c39 55#include <sys/stat.h>
c906108c 56#include <ctype.h>
015a42b4 57#include "cp-abi.h"
71c25dea 58#include "cp-support.h"
76727919 59#include "observable.h"
3a40aaa0 60#include "solist.h"
9a044a89
TT
61#include "macrotab.h"
62#include "macroscope.h"
c906108c 63
270140bd 64#include "parser-defs.h"
ef0b411a 65#include "completer.h"
5ed8105e 66#include "progspace-and-thread.h"
268a13a5 67#include "gdbsupport/gdb_optional.h"
bbf2f4df 68#include "filename-seen-cache.h"
46a62268 69#include "arch-utils.h"
b9c04fb2 70#include <algorithm>
7bb43059 71#include "gdbsupport/gdb_string_view.h"
268a13a5 72#include "gdbsupport/pathstuff.h"
1a6ff1a9 73#include "gdbsupport/common-utils.h"
ccefe4c4 74
ff6c39cf 75/* Forward declarations for local functions. */
c906108c 76
0b39b52e 77static void rbreak_command (const char *, int);
c906108c 78
f8eba3c6 79static int find_line_common (struct linetable *, int, int *, int);
c906108c 80
d12307c1
PMR
81static struct block_symbol
82 lookup_symbol_aux (const char *name,
de63c46b 83 symbol_name_match_type match_type,
d12307c1
PMR
84 const struct block *block,
85 const domain_enum domain,
86 enum language language,
87 struct field_of_this_result *);
fba7f19c 88
e4051eeb 89static
d12307c1 90struct block_symbol lookup_local_symbol (const char *name,
de63c46b 91 symbol_name_match_type match_type,
d12307c1
PMR
92 const struct block *block,
93 const domain_enum domain,
94 enum language language);
8155455b 95
d12307c1 96static struct block_symbol
c32e6a04
CB
97 lookup_symbol_in_objfile (struct objfile *objfile,
98 enum block_enum block_index,
fe2a438d 99 const char *name, const domain_enum domain);
c906108c 100
32ac0d11
TT
101/* Type of the data stored on the program space. */
102
103struct main_info
104{
a32ad8c5
TT
105 main_info () = default;
106
107 ~main_info ()
108 {
109 xfree (name_of_main);
110 }
111
32ac0d11
TT
112 /* Name of "main". */
113
a32ad8c5 114 char *name_of_main = nullptr;
32ac0d11
TT
115
116 /* Language of "main". */
117
a32ad8c5 118 enum language language_of_main = language_unknown;
32ac0d11
TT
119};
120
a32ad8c5
TT
121/* Program space key for finding name and language of "main". */
122
123static const program_space_key<main_info> main_progspace_key;
124
f57d2163
DE
125/* The default symbol cache size.
126 There is no extra cpu cost for large N (except when flushing the cache,
127 which is rare). The value here is just a first attempt. A better default
128 value may be higher or lower. A prime number can make up for a bad hash
129 computation, so that's why the number is what it is. */
130#define DEFAULT_SYMBOL_CACHE_SIZE 1021
131
132/* The maximum symbol cache size.
133 There's no method to the decision of what value to use here, other than
134 there's no point in allowing a user typo to make gdb consume all memory. */
135#define MAX_SYMBOL_CACHE_SIZE (1024*1024)
136
137/* symbol_cache_lookup returns this if a previous lookup failed to find the
138 symbol in any objfile. */
d12307c1
PMR
139#define SYMBOL_LOOKUP_FAILED \
140 ((struct block_symbol) {(struct symbol *) 1, NULL})
141#define SYMBOL_LOOKUP_FAILED_P(SIB) (SIB.symbol == (struct symbol *) 1)
f57d2163
DE
142
143/* Recording lookups that don't find the symbol is just as important, if not
144 more so, than recording found symbols. */
145
146enum symbol_cache_slot_state
147{
148 SYMBOL_SLOT_UNUSED,
149 SYMBOL_SLOT_NOT_FOUND,
150 SYMBOL_SLOT_FOUND
151};
152
52059ffd
TT
153struct symbol_cache_slot
154{
155 enum symbol_cache_slot_state state;
156
157 /* The objfile that was current when the symbol was looked up.
158 This is only needed for global blocks, but for simplicity's sake
159 we allocate the space for both. If data shows the extra space used
160 for static blocks is a problem, we can split things up then.
161
162 Global blocks need cache lookup to include the objfile context because
163 we need to account for gdbarch_iterate_over_objfiles_in_search_order
164 which can traverse objfiles in, effectively, any order, depending on
165 the current objfile, thus affecting which symbol is found. Normally,
166 only the current objfile is searched first, and then the rest are
167 searched in recorded order; but putting cache lookup inside
168 gdbarch_iterate_over_objfiles_in_search_order would be awkward.
169 Instead we just make the current objfile part of the context of
170 cache lookup. This means we can record the same symbol multiple times,
171 each with a different "current objfile" that was in effect when the
172 lookup was saved in the cache, but cache space is pretty cheap. */
173 const struct objfile *objfile_context;
174
175 union
176 {
d12307c1 177 struct block_symbol found;
52059ffd
TT
178 struct
179 {
180 char *name;
181 domain_enum domain;
182 } not_found;
183 } value;
184};
185
f57d2163
DE
186/* Symbols don't specify global vs static block.
187 So keep them in separate caches. */
188
189struct block_symbol_cache
190{
191 unsigned int hits;
192 unsigned int misses;
193 unsigned int collisions;
194
195 /* SYMBOLS is a variable length array of this size.
196 One can imagine that in general one cache (global/static) should be a
197 fraction of the size of the other, but there's no data at the moment
198 on which to decide. */
199 unsigned int size;
200
52059ffd 201 struct symbol_cache_slot symbols[1];
f57d2163
DE
202};
203
204/* The symbol cache.
205
206 Searching for symbols in the static and global blocks over multiple objfiles
207 again and again can be slow, as can searching very big objfiles. This is a
208 simple cache to improve symbol lookup performance, which is critical to
209 overall gdb performance.
210
211 Symbols are hashed on the name, its domain, and block.
212 They are also hashed on their objfile for objfile-specific lookups. */
213
214struct symbol_cache
215{
3017b94d
TT
216 symbol_cache () = default;
217
218 ~symbol_cache ()
219 {
220 xfree (global_symbols);
221 xfree (static_symbols);
222 }
223
224 struct block_symbol_cache *global_symbols = nullptr;
225 struct block_symbol_cache *static_symbols = nullptr;
f57d2163
DE
226};
227
3017b94d
TT
228/* Program space key for finding its symbol cache. */
229
230static const program_space_key<symbol_cache> symbol_cache_key;
231
45cfd468 232/* When non-zero, print debugging messages related to symtab creation. */
db0fec5c 233unsigned int symtab_create_debug = 0;
45cfd468 234
cc485e62
DE
235/* When non-zero, print debugging messages related to symbol lookup. */
236unsigned int symbol_lookup_debug = 0;
237
f57d2163
DE
238/* The size of the cache is staged here. */
239static unsigned int new_symbol_cache_size = DEFAULT_SYMBOL_CACHE_SIZE;
240
241/* The current value of the symbol cache size.
242 This is saved so that if the user enters a value too big we can restore
243 the original value from here. */
244static unsigned int symbol_cache_size = DEFAULT_SYMBOL_CACHE_SIZE;
245
491144b5 246/* True if a file may be known by two different basenames.
c011a4f4
DE
247 This is the uncommon case, and significantly slows down gdb.
248 Default set to "off" to not slow down the common case. */
491144b5 249bool basenames_may_differ = false;
c011a4f4 250
717d2f5a
JB
251/* Allow the user to configure the debugger behavior with respect
252 to multiple-choice menus when more than one symbol matches during
253 a symbol lookup. */
254
7fc830e2
MK
255const char multiple_symbols_ask[] = "ask";
256const char multiple_symbols_all[] = "all";
257const char multiple_symbols_cancel[] = "cancel";
40478521 258static const char *const multiple_symbols_modes[] =
717d2f5a
JB
259{
260 multiple_symbols_ask,
261 multiple_symbols_all,
262 multiple_symbols_cancel,
263 NULL
264};
265static const char *multiple_symbols_mode = multiple_symbols_all;
266
267/* Read-only accessor to AUTO_SELECT_MODE. */
268
269const char *
270multiple_symbols_select_mode (void)
271{
272 return multiple_symbols_mode;
273}
274
20c681d1
DE
275/* Return the name of a domain_enum. */
276
277const char *
278domain_name (domain_enum e)
279{
280 switch (e)
281 {
282 case UNDEF_DOMAIN: return "UNDEF_DOMAIN";
283 case VAR_DOMAIN: return "VAR_DOMAIN";
284 case STRUCT_DOMAIN: return "STRUCT_DOMAIN";
540feddf 285 case MODULE_DOMAIN: return "MODULE_DOMAIN";
20c681d1
DE
286 case LABEL_DOMAIN: return "LABEL_DOMAIN";
287 case COMMON_BLOCK_DOMAIN: return "COMMON_BLOCK_DOMAIN";
288 default: gdb_assert_not_reached ("bad domain_enum");
289 }
290}
291
292/* Return the name of a search_domain . */
293
294const char *
295search_domain_name (enum search_domain e)
296{
297 switch (e)
298 {
299 case VARIABLES_DOMAIN: return "VARIABLES_DOMAIN";
300 case FUNCTIONS_DOMAIN: return "FUNCTIONS_DOMAIN";
301 case TYPES_DOMAIN: return "TYPES_DOMAIN";
302 case ALL_DOMAIN: return "ALL_DOMAIN";
303 default: gdb_assert_not_reached ("bad search_domain");
304 }
305}
306
43f3e411 307/* See symtab.h. */
db0fec5c 308
43f3e411
DE
309struct symtab *
310compunit_primary_filetab (const struct compunit_symtab *cust)
db0fec5c 311{
43f3e411 312 gdb_assert (COMPUNIT_FILETABS (cust) != NULL);
db0fec5c 313
43f3e411
DE
314 /* The primary file symtab is the first one in the list. */
315 return COMPUNIT_FILETABS (cust);
316}
317
318/* See symtab.h. */
319
320enum language
321compunit_language (const struct compunit_symtab *cust)
322{
323 struct symtab *symtab = compunit_primary_filetab (cust);
324
325/* The language of the compunit symtab is the language of its primary
326 source file. */
327 return SYMTAB_LANGUAGE (symtab);
db0fec5c
DE
328}
329
1ed9f74e
PW
330/* See symtab.h. */
331
332bool
333minimal_symbol::data_p () const
334{
335 return type == mst_data
336 || type == mst_bss
337 || type == mst_abs
338 || type == mst_file_data
339 || type == mst_file_bss;
340}
341
342/* See symtab.h. */
343
344bool
345minimal_symbol::text_p () const
346{
347 return type == mst_text
348 || type == mst_text_gnu_ifunc
349 || type == mst_data_gnu_ifunc
350 || type == mst_slot_got_plt
351 || type == mst_solib_trampoline
352 || type == mst_file_text;
353}
354
4aac40c8
TT
355/* See whether FILENAME matches SEARCH_NAME using the rule that we
356 advertise to the user. (The manual's description of linespecs
af529f8f
JK
357 describes what we advertise). Returns true if they match, false
358 otherwise. */
4aac40c8 359
ececd218 360bool
b57a636e 361compare_filenames_for_search (const char *filename, const char *search_name)
4aac40c8
TT
362{
363 int len = strlen (filename);
b57a636e 364 size_t search_len = strlen (search_name);
4aac40c8
TT
365
366 if (len < search_len)
ececd218 367 return false;
4aac40c8
TT
368
369 /* The tail of FILENAME must match. */
370 if (FILENAME_CMP (filename + len - search_len, search_name) != 0)
ececd218 371 return false;
4aac40c8
TT
372
373 /* Either the names must completely match, or the character
374 preceding the trailing SEARCH_NAME segment of FILENAME must be a
d84fca2c
JK
375 directory separator.
376
af529f8f
JK
377 The check !IS_ABSOLUTE_PATH ensures SEARCH_NAME "/dir/file.c"
378 cannot match FILENAME "/path//dir/file.c" - as user has requested
379 absolute path. The sama applies for "c:\file.c" possibly
380 incorrectly hypothetically matching "d:\dir\c:\file.c".
381
d84fca2c
JK
382 The HAS_DRIVE_SPEC purpose is to make FILENAME "c:file.c"
383 compatible with SEARCH_NAME "file.c". In such case a compiler had
384 to put the "c:file.c" name into debug info. Such compatibility
385 works only on GDB built for DOS host. */
4aac40c8 386 return (len == search_len
af529f8f
JK
387 || (!IS_ABSOLUTE_PATH (search_name)
388 && IS_DIR_SEPARATOR (filename[len - search_len - 1]))
4aac40c8
TT
389 || (HAS_DRIVE_SPEC (filename)
390 && STRIP_DRIVE_SPEC (filename) == &filename[len - search_len]));
391}
392
cce0e923
DE
393/* Same as compare_filenames_for_search, but for glob-style patterns.
394 Heads up on the order of the arguments. They match the order of
395 compare_filenames_for_search, but it's the opposite of the order of
396 arguments to gdb_filename_fnmatch. */
397
ececd218 398bool
cce0e923
DE
399compare_glob_filenames_for_search (const char *filename,
400 const char *search_name)
401{
402 /* We rely on the property of glob-style patterns with FNM_FILE_NAME that
403 all /s have to be explicitly specified. */
404 int file_path_elements = count_path_elements (filename);
405 int search_path_elements = count_path_elements (search_name);
406
407 if (search_path_elements > file_path_elements)
ececd218 408 return false;
cce0e923
DE
409
410 if (IS_ABSOLUTE_PATH (search_name))
411 {
412 return (search_path_elements == file_path_elements
413 && gdb_filename_fnmatch (search_name, filename,
414 FNM_FILE_NAME | FNM_NOESCAPE) == 0);
415 }
416
417 {
418 const char *file_to_compare
419 = strip_leading_path_elements (filename,
420 file_path_elements - search_path_elements);
421
422 return gdb_filename_fnmatch (search_name, file_to_compare,
423 FNM_FILE_NAME | FNM_NOESCAPE) == 0;
424 }
425}
426
f8eba3c6
TT
427/* Check for a symtab of a specific name by searching some symtabs.
428 This is a helper function for callbacks of iterate_over_symtabs.
c906108c 429
b2d23133
DE
430 If NAME is not absolute, then REAL_PATH is NULL
431 If NAME is absolute, then REAL_PATH is the gdb_realpath form of NAME.
432
14bc53a8
PA
433 The return value, NAME, REAL_PATH and CALLBACK are identical to the
434 `map_symtabs_matching_filename' method of quick_symbol_functions.
f8eba3c6 435
43f3e411
DE
436 FIRST and AFTER_LAST indicate the range of compunit symtabs to search.
437 Each symtab within the specified compunit symtab is also searched.
438 AFTER_LAST is one past the last compunit symtab to search; NULL means to
f8eba3c6
TT
439 search until the end of the list. */
440
14bc53a8 441bool
f8eba3c6 442iterate_over_some_symtabs (const char *name,
f8eba3c6 443 const char *real_path,
43f3e411 444 struct compunit_symtab *first,
14bc53a8
PA
445 struct compunit_symtab *after_last,
446 gdb::function_view<bool (symtab *)> callback)
c906108c 447{
43f3e411 448 struct compunit_symtab *cust;
c011a4f4 449 const char* base_name = lbasename (name);
1f84b619 450
43f3e411 451 for (cust = first; cust != NULL && cust != after_last; cust = cust->next)
f079a2e5 452 {
5accd1a0 453 for (symtab *s : compunit_filetabs (cust))
a94e8645 454 {
43f3e411
DE
455 if (compare_filenames_for_search (s->filename, name))
456 {
14bc53a8
PA
457 if (callback (s))
458 return true;
43f3e411
DE
459 continue;
460 }
a94e8645 461
43f3e411
DE
462 /* Before we invoke realpath, which can get expensive when many
463 files are involved, do a quick comparison of the basenames. */
464 if (! basenames_may_differ
465 && FILENAME_CMP (base_name, lbasename (s->filename)) != 0)
466 continue;
a94e8645 467
43f3e411 468 if (compare_filenames_for_search (symtab_to_fullname (s), name))
a94e8645 469 {
14bc53a8
PA
470 if (callback (s))
471 return true;
a94e8645
DE
472 continue;
473 }
43f3e411
DE
474
475 /* If the user gave us an absolute path, try to find the file in
476 this symtab and use its absolute path. */
477 if (real_path != NULL)
478 {
479 const char *fullname = symtab_to_fullname (s);
480
481 gdb_assert (IS_ABSOLUTE_PATH (real_path));
482 gdb_assert (IS_ABSOLUTE_PATH (name));
7e785608
TV
483 gdb::unique_xmalloc_ptr<char> fullname_real_path
484 = gdb_realpath (fullname);
485 fullname = fullname_real_path.get ();
43f3e411
DE
486 if (FILENAME_CMP (real_path, fullname) == 0)
487 {
14bc53a8
PA
488 if (callback (s))
489 return true;
43f3e411
DE
490 continue;
491 }
492 }
a94e8645 493 }
f8eba3c6 494 }
58d370e0 495
14bc53a8 496 return false;
f8eba3c6
TT
497}
498
499/* Check for a symtab of a specific name; first in symtabs, then in
500 psymtabs. *If* there is no '/' in the name, a match after a '/'
501 in the symtab filename will also work.
502
14bc53a8
PA
503 Calls CALLBACK with each symtab that is found. If CALLBACK returns
504 true, the search stops. */
f8eba3c6
TT
505
506void
507iterate_over_symtabs (const char *name,
14bc53a8 508 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 509{
14bc53a8 510 gdb::unique_xmalloc_ptr<char> real_path;
f8eba3c6
TT
511
512 /* Here we are interested in canonicalizing an absolute path, not
513 absolutizing a relative path. */
514 if (IS_ABSOLUTE_PATH (name))
515 {
14278e1f 516 real_path = gdb_realpath (name);
14bc53a8 517 gdb_assert (IS_ABSOLUTE_PATH (real_path.get ()));
f8eba3c6
TT
518 }
519
2030c079 520 for (objfile *objfile : current_program_space->objfiles ())
14bc53a8
PA
521 {
522 if (iterate_over_some_symtabs (name, real_path.get (),
523 objfile->compunit_symtabs, NULL,
524 callback))
f8eba3c6 525 return;
14bc53a8 526 }
f8eba3c6 527
c906108c
SS
528 /* Same search rules as above apply here, but now we look thru the
529 psymtabs. */
530
2030c079 531 for (objfile *objfile : current_program_space->objfiles ())
14bc53a8
PA
532 {
533 if (objfile->sf
534 && objfile->sf->qf->map_symtabs_matching_filename (objfile,
535 name,
536 real_path.get (),
537 callback))
f8eba3c6 538 return;
14bc53a8 539 }
c906108c 540}
f8eba3c6
TT
541
542/* A wrapper for iterate_over_symtabs that returns the first matching
543 symtab, or NULL. */
544
545struct symtab *
546lookup_symtab (const char *name)
547{
548 struct symtab *result = NULL;
549
14bc53a8
PA
550 iterate_over_symtabs (name, [&] (symtab *symtab)
551 {
552 result = symtab;
553 return true;
554 });
555
f8eba3c6
TT
556 return result;
557}
558
c906108c
SS
559\f
560/* Mangle a GDB method stub type. This actually reassembles the pieces of the
561 full method name, which consist of the class name (from T), the unadorned
562 method name from METHOD_ID, and the signature for the specific overload,
c378eb4e 563 specified by SIGNATURE_ID. Note that this function is g++ specific. */
c906108c
SS
564
565char *
fba45db2 566gdb_mangle_name (struct type *type, int method_id, int signature_id)
c906108c
SS
567{
568 int mangled_name_len;
569 char *mangled_name;
570 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
571 struct fn_field *method = &f[signature_id];
0d5cff50 572 const char *field_name = TYPE_FN_FIELDLIST_NAME (type, method_id);
1d06ead6 573 const char *physname = TYPE_FN_FIELD_PHYSNAME (f, signature_id);
a737d952 574 const char *newname = TYPE_NAME (type);
c906108c
SS
575
576 /* Does the form of physname indicate that it is the full mangled name
577 of a constructor (not just the args)? */
578 int is_full_physname_constructor;
579
580 int is_constructor;
015a42b4 581 int is_destructor = is_destructor_name (physname);
c906108c 582 /* Need a new type prefix. */
e6a959d6
PA
583 const char *const_prefix = method->is_const ? "C" : "";
584 const char *volatile_prefix = method->is_volatile ? "V" : "";
c906108c
SS
585 char buf[20];
586 int len = (newname == NULL ? 0 : strlen (newname));
587
43630227
PS
588 /* Nothing to do if physname already contains a fully mangled v3 abi name
589 or an operator name. */
590 if ((physname[0] == '_' && physname[1] == 'Z')
591 || is_operator_name (field_name))
235d1e03
EZ
592 return xstrdup (physname);
593
015a42b4 594 is_full_physname_constructor = is_constructor_name (physname);
c906108c 595
3e43a32a
MS
596 is_constructor = is_full_physname_constructor
597 || (newname && strcmp (field_name, newname) == 0);
c906108c
SS
598
599 if (!is_destructor)
61012eef 600 is_destructor = (startswith (physname, "__dt"));
c906108c
SS
601
602 if (is_destructor || is_full_physname_constructor)
603 {
c5aa993b
JM
604 mangled_name = (char *) xmalloc (strlen (physname) + 1);
605 strcpy (mangled_name, physname);
c906108c
SS
606 return mangled_name;
607 }
608
609 if (len == 0)
610 {
8c042590 611 xsnprintf (buf, sizeof (buf), "__%s%s", const_prefix, volatile_prefix);
c906108c
SS
612 }
613 else if (physname[0] == 't' || physname[0] == 'Q')
614 {
615 /* The physname for template and qualified methods already includes
c5aa993b 616 the class name. */
8c042590 617 xsnprintf (buf, sizeof (buf), "__%s%s", const_prefix, volatile_prefix);
c906108c
SS
618 newname = NULL;
619 len = 0;
620 }
621 else
622 {
8c042590
PM
623 xsnprintf (buf, sizeof (buf), "__%s%s%d", const_prefix,
624 volatile_prefix, len);
c906108c
SS
625 }
626 mangled_name_len = ((is_constructor ? 0 : strlen (field_name))
235d1e03 627 + strlen (buf) + len + strlen (physname) + 1);
c906108c 628
433759f7
MS
629 mangled_name = (char *) xmalloc (mangled_name_len);
630 if (is_constructor)
631 mangled_name[0] = '\0';
632 else
633 strcpy (mangled_name, field_name);
634
c906108c
SS
635 strcat (mangled_name, buf);
636 /* If the class doesn't have a name, i.e. newname NULL, then we just
637 mangle it using 0 for the length of the class. Thus it gets mangled
c378eb4e 638 as something starting with `::' rather than `classname::'. */
c906108c
SS
639 if (newname != NULL)
640 strcat (mangled_name, newname);
641
642 strcat (mangled_name, physname);
643 return (mangled_name);
644}
12af6855 645
b250c185 646/* Set the demangled name of GSYMBOL to NAME. NAME must be already
7c5fdd25 647 correctly allocated. */
eca864fe 648
b250c185
SW
649void
650symbol_set_demangled_name (struct general_symbol_info *gsymbol,
cfc594ee 651 const char *name,
ccde22c0 652 struct obstack *obstack)
b250c185 653{
7c5fdd25 654 if (gsymbol->language == language_ada)
f85f34ed
TT
655 {
656 if (name == NULL)
657 {
658 gsymbol->ada_mangled = 0;
659 gsymbol->language_specific.obstack = obstack;
660 }
661 else
662 {
663 gsymbol->ada_mangled = 1;
615b3f62 664 gsymbol->language_specific.demangled_name = name;
f85f34ed
TT
665 }
666 }
29df156d 667 else
615b3f62 668 gsymbol->language_specific.demangled_name = name;
b250c185
SW
669}
670
671/* Return the demangled name of GSYMBOL. */
eca864fe 672
0d5cff50 673const char *
b250c185
SW
674symbol_get_demangled_name (const struct general_symbol_info *gsymbol)
675{
7c5fdd25 676 if (gsymbol->language == language_ada)
f85f34ed
TT
677 {
678 if (!gsymbol->ada_mangled)
679 return NULL;
680 /* Fall through. */
681 }
682
615b3f62 683 return gsymbol->language_specific.demangled_name;
b250c185
SW
684}
685
12af6855 686\f
89aad1f9 687/* Initialize the language dependent portion of a symbol
c378eb4e 688 depending upon the language for the symbol. */
eca864fe 689
89aad1f9 690void
33e5013e 691symbol_set_language (struct general_symbol_info *gsymbol,
f85f34ed
TT
692 enum language language,
693 struct obstack *obstack)
89aad1f9
EZ
694{
695 gsymbol->language = language;
7c5fdd25
DE
696 if (gsymbol->language == language_cplus
697 || gsymbol->language == language_d
a766d390 698 || gsymbol->language == language_go
f55ee35c
JK
699 || gsymbol->language == language_objc
700 || gsymbol->language == language_fortran)
89aad1f9 701 {
f85f34ed
TT
702 symbol_set_demangled_name (gsymbol, NULL, obstack);
703 }
704 else if (gsymbol->language == language_ada)
705 {
706 gdb_assert (gsymbol->ada_mangled == 0);
707 gsymbol->language_specific.obstack = obstack;
89aad1f9 708 }
89aad1f9
EZ
709 else
710 {
711 memset (&gsymbol->language_specific, 0,
712 sizeof (gsymbol->language_specific));
713 }
714}
715
2de7ced7
DJ
716/* Functions to initialize a symbol's mangled name. */
717
04a679b8
TT
718/* Objects of this type are stored in the demangled name hash table. */
719struct demangled_name_entry
720{
3a494279
CB
721 demangled_name_entry (gdb::string_view mangled_name)
722 : mangled (mangled_name) {}
723
7bb43059 724 gdb::string_view mangled;
403772ef 725 enum language language;
5396ae17 726 gdb::unique_xmalloc_ptr<char> demangled;
04a679b8
TT
727};
728
729/* Hash function for the demangled name hash. */
eca864fe 730
04a679b8
TT
731static hashval_t
732hash_demangled_name_entry (const void *data)
733{
19ba03f4
SM
734 const struct demangled_name_entry *e
735 = (const struct demangled_name_entry *) data;
433759f7 736
1a6ff1a9 737 return fast_hash (e->mangled.data (), e->mangled.length ());
04a679b8
TT
738}
739
740/* Equality function for the demangled name hash. */
eca864fe 741
04a679b8
TT
742static int
743eq_demangled_name_entry (const void *a, const void *b)
744{
19ba03f4
SM
745 const struct demangled_name_entry *da
746 = (const struct demangled_name_entry *) a;
747 const struct demangled_name_entry *db
748 = (const struct demangled_name_entry *) b;
433759f7 749
7bb43059 750 return da->mangled == db->mangled;
04a679b8
TT
751}
752
3a494279
CB
753static void
754free_demangled_name_entry (void *data)
755{
756 struct demangled_name_entry *e
757 = (struct demangled_name_entry *) data;
758
759 e->~demangled_name_entry();
760}
761
2de7ced7
DJ
762/* Create the hash table used for demangled names. Each hash entry is
763 a pair of strings; one for the mangled name and one for the demangled
764 name. The entry is hashed via just the mangled name. */
765
766static void
0f14768a 767create_demangled_names_hash (struct objfile_per_bfd_storage *per_bfd)
2de7ced7
DJ
768{
769 /* Choose 256 as the starting size of the hash table, somewhat arbitrarily.
9af17804 770 The hash table code will round this up to the next prime number.
2de7ced7
DJ
771 Choosing a much larger table size wastes memory, and saves only about
772 1% in symbol reading. */
773
db92718b 774 per_bfd->demangled_names_hash.reset (htab_create_alloc
04a679b8 775 (256, hash_demangled_name_entry, eq_demangled_name_entry,
3a494279 776 free_demangled_name_entry, xcalloc, xfree));
2de7ced7 777}
12af6855 778
2de7ced7 779/* Try to determine the demangled name for a symbol, based on the
12af6855
JB
780 language of that symbol. If the language is set to language_auto,
781 it will attempt to find any demangling algorithm that works and
2de7ced7
DJ
782 then set the language appropriately. The returned name is allocated
783 by the demangler and should be xfree'd. */
12af6855 784
2de7ced7
DJ
785static char *
786symbol_find_demangled_name (struct general_symbol_info *gsymbol,
787 const char *mangled)
12af6855 788{
12af6855 789 char *demangled = NULL;
8b302db8 790 int i;
12af6855
JB
791
792 if (gsymbol->language == language_unknown)
793 gsymbol->language = language_auto;
1bae87b9 794
8b302db8 795 if (gsymbol->language != language_auto)
1bae87b9 796 {
8b302db8
TT
797 const struct language_defn *lang = language_def (gsymbol->language);
798
799 language_sniff_from_mangled_name (lang, mangled, &demangled);
800 return demangled;
6aecb9c2 801 }
8b302db8
TT
802
803 for (i = language_unknown; i < nr_languages; ++i)
a766d390 804 {
8b302db8
TT
805 enum language l = (enum language) i;
806 const struct language_defn *lang = language_def (l);
807
808 if (language_sniff_from_mangled_name (lang, mangled, &demangled))
a766d390 809 {
8b302db8 810 gsymbol->language = l;
a766d390
DE
811 return demangled;
812 }
813 }
814
2de7ced7
DJ
815 return NULL;
816}
817
980cae7a 818/* Set both the mangled and demangled (if any) names for GSYMBOL based
04a679b8
TT
819 on LINKAGE_NAME and LEN. Ordinarily, NAME is copied onto the
820 objfile's obstack; but if COPY_NAME is 0 and if NAME is
821 NUL-terminated, then this function assumes that NAME is already
822 correctly saved (either permanently or with a lifetime tied to the
823 objfile), and it will not be copied.
824
825 The hash table corresponding to OBJFILE is used, and the memory
84a1243b 826 comes from the per-BFD storage_obstack. LINKAGE_NAME is copied,
04a679b8 827 so the pointer can be discarded after calling this function. */
2de7ced7
DJ
828
829void
830symbol_set_names (struct general_symbol_info *gsymbol,
67547d89 831 const char *linkage_name, int len, bool copy_name,
1d94a5a3 832 struct objfile_per_bfd_storage *per_bfd)
2de7ced7 833{
04a679b8 834 struct demangled_name_entry **slot;
2de7ced7 835
b06ead72
JB
836 if (gsymbol->language == language_ada)
837 {
838 /* In Ada, we do the symbol lookups using the mangled name, so
9c37b5ae 839 we can save some space by not storing the demangled name. */
92174eea 840 if (!copy_name)
0d5cff50 841 gsymbol->name = linkage_name;
04a679b8
TT
842 else
843 {
224c3ddb
SM
844 char *name = (char *) obstack_alloc (&per_bfd->storage_obstack,
845 len + 1);
0d5cff50
DE
846
847 memcpy (name, linkage_name, len);
848 name[len] = '\0';
849 gsymbol->name = name;
04a679b8 850 }
84a1243b 851 symbol_set_demangled_name (gsymbol, NULL, &per_bfd->storage_obstack);
b06ead72
JB
852
853 return;
854 }
855
84a1243b 856 if (per_bfd->demangled_names_hash == NULL)
0f14768a 857 create_demangled_names_hash (per_bfd);
04a679b8 858
0c921b21 859 struct demangled_name_entry entry (gdb::string_view (linkage_name, len));
04a679b8 860 slot = ((struct demangled_name_entry **)
db92718b 861 htab_find_slot (per_bfd->demangled_names_hash.get (),
04a679b8 862 &entry, INSERT));
2de7ced7
DJ
863
864 /* If this name is not in the hash table, add it. */
a766d390
DE
865 if (*slot == NULL
866 /* A C version of the symbol may have already snuck into the table.
867 This happens to, e.g., main.init (__go_init_main). Cope. */
5396ae17 868 || (gsymbol->language == language_go && (*slot)->demangled == nullptr))
2de7ced7 869 {
0c921b21
CB
870 /* A 0-terminated copy of the linkage name. Callers must set COPY_NAME
871 to true if the string might not be nullterminated. We have to make
872 this copy because demangling needs a nullterminated string. */
873 const char *linkage_name_copy;
874 if (copy_name)
875 {
876 char *alloc_name = (char *) alloca (len + 1);
877 memcpy (alloc_name, linkage_name, len);
878 alloc_name[len] = '\0';
879
880 linkage_name_copy = alloc_name;
881 }
882 else
883 linkage_name_copy = linkage_name;
884
5396ae17
CB
885 gdb::unique_xmalloc_ptr<char> demangled_name_ptr
886 (symbol_find_demangled_name (gsymbol, linkage_name_copy));
2de7ced7 887
04a679b8 888 /* Suppose we have demangled_name==NULL, copy_name==0, and
9c37b5ae 889 linkage_name_copy==linkage_name. In this case, we already have the
04a679b8
TT
890 mangled name saved, and we don't have a demangled name. So,
891 you might think we could save a little space by not recording
892 this in the hash table at all.
5396ae17 893
04a679b8
TT
894 It turns out that it is actually important to still save such
895 an entry in the hash table, because storing this name gives
705b5767 896 us better bcache hit rates for partial symbols. */
0c921b21 897 if (!copy_name)
04a679b8 898 {
224c3ddb
SM
899 *slot
900 = ((struct demangled_name_entry *)
901 obstack_alloc (&per_bfd->storage_obstack,
5396ae17 902 sizeof (demangled_name_entry)));
3a494279
CB
903 new (*slot) demangled_name_entry
904 (gdb::string_view (linkage_name, len));
04a679b8
TT
905 }
906 else
907 {
908 /* If we must copy the mangled name, put it directly after
5396ae17 909 the struct so we can have a single allocation. */
224c3ddb
SM
910 *slot
911 = ((struct demangled_name_entry *)
912 obstack_alloc (&per_bfd->storage_obstack,
5396ae17
CB
913 sizeof (demangled_name_entry) + len + 1));
914 char *mangled_ptr = reinterpret_cast<char *> (*slot + 1);
0c921b21
CB
915 memcpy (mangled_ptr, linkage_name, len);
916 mangled_ptr [len] = '\0';
3a494279
CB
917 new (*slot) demangled_name_entry
918 (gdb::string_view (mangled_ptr, len));
04a679b8 919 }
5396ae17 920 (*slot)->demangled = std::move (demangled_name_ptr);
e99f9db0 921 (*slot)->language = gsymbol->language;
2de7ced7 922 }
e99f9db0
TV
923 else if (gsymbol->language == language_unknown
924 || gsymbol->language == language_auto)
925 gsymbol->language = (*slot)->language;
2de7ced7 926
7bb43059 927 gsymbol->name = (*slot)->mangled.data ();
5396ae17
CB
928 if ((*slot)->demangled != nullptr)
929 symbol_set_demangled_name (gsymbol, (*slot)->demangled.get (),
84a1243b 930 &per_bfd->storage_obstack);
2de7ced7 931 else
84a1243b 932 symbol_set_demangled_name (gsymbol, NULL, &per_bfd->storage_obstack);
2de7ced7
DJ
933}
934
22abf04a
DC
935/* Return the source code name of a symbol. In languages where
936 demangling is necessary, this is the demangled name. */
937
0d5cff50 938const char *
22abf04a
DC
939symbol_natural_name (const struct general_symbol_info *gsymbol)
940{
9af17804 941 switch (gsymbol->language)
22abf04a 942 {
1f8173e6 943 case language_cplus:
6aecb9c2 944 case language_d:
a766d390 945 case language_go:
1f8173e6 946 case language_objc:
f55ee35c 947 case language_fortran:
b250c185
SW
948 if (symbol_get_demangled_name (gsymbol) != NULL)
949 return symbol_get_demangled_name (gsymbol);
1f8173e6
PH
950 break;
951 case language_ada:
f85f34ed 952 return ada_decode_symbol (gsymbol);
1f8173e6
PH
953 default:
954 break;
22abf04a 955 }
1f8173e6 956 return gsymbol->name;
22abf04a
DC
957}
958
9cc0d196 959/* Return the demangled name for a symbol based on the language for
c378eb4e 960 that symbol. If no demangled name exists, return NULL. */
eca864fe 961
0d5cff50 962const char *
df8a16a1 963symbol_demangled_name (const struct general_symbol_info *gsymbol)
9cc0d196 964{
c6e5ee5e
SDJ
965 const char *dem_name = NULL;
966
9af17804 967 switch (gsymbol->language)
1f8173e6
PH
968 {
969 case language_cplus:
6aecb9c2 970 case language_d:
a766d390 971 case language_go:
1f8173e6 972 case language_objc:
f55ee35c 973 case language_fortran:
c6e5ee5e 974 dem_name = symbol_get_demangled_name (gsymbol);
1f8173e6
PH
975 break;
976 case language_ada:
f85f34ed 977 dem_name = ada_decode_symbol (gsymbol);
1f8173e6
PH
978 break;
979 default:
980 break;
981 }
c6e5ee5e 982 return dem_name;
9cc0d196 983}
fe39c653 984
4725b721
PH
985/* Return the search name of a symbol---generally the demangled or
986 linkage name of the symbol, depending on how it will be searched for.
9af17804 987 If there is no distinct demangled name, then returns the same value
c378eb4e 988 (same pointer) as SYMBOL_LINKAGE_NAME. */
eca864fe 989
0d5cff50 990const char *
fc062ac6
JB
991symbol_search_name (const struct general_symbol_info *gsymbol)
992{
1f8173e6
PH
993 if (gsymbol->language == language_ada)
994 return gsymbol->name;
995 else
996 return symbol_natural_name (gsymbol);
4725b721 997}
b5ec771e
PA
998
999/* See symtab.h. */
1000
1001bool
1002symbol_matches_search_name (const struct general_symbol_info *gsymbol,
1003 const lookup_name_info &name)
1004{
1005 symbol_name_matcher_ftype *name_match
618daa93 1006 = get_symbol_name_matcher (language_def (gsymbol->language), name);
b5ec771e
PA
1007 return name_match (symbol_search_name (gsymbol), name, NULL);
1008}
1009
c906108c
SS
1010\f
1011
ececd218 1012/* Return true if the two sections are the same, or if they could
94277a38
DJ
1013 plausibly be copies of each other, one in an original object
1014 file and another in a separated debug file. */
1015
ececd218 1016bool
714835d5
UW
1017matching_obj_sections (struct obj_section *obj_first,
1018 struct obj_section *obj_second)
94277a38 1019{
714835d5
UW
1020 asection *first = obj_first? obj_first->the_bfd_section : NULL;
1021 asection *second = obj_second? obj_second->the_bfd_section : NULL;
94277a38
DJ
1022
1023 /* If they're the same section, then they match. */
1024 if (first == second)
ececd218 1025 return true;
94277a38
DJ
1026
1027 /* If either is NULL, give up. */
1028 if (first == NULL || second == NULL)
ececd218 1029 return false;
94277a38
DJ
1030
1031 /* This doesn't apply to absolute symbols. */
1032 if (first->owner == NULL || second->owner == NULL)
ececd218 1033 return false;
94277a38
DJ
1034
1035 /* If they're in the same object file, they must be different sections. */
1036 if (first->owner == second->owner)
ececd218 1037 return false;
94277a38
DJ
1038
1039 /* Check whether the two sections are potentially corresponding. They must
1040 have the same size, address, and name. We can't compare section indexes,
1041 which would be more reliable, because some sections may have been
1042 stripped. */
fd361982 1043 if (bfd_section_size (first) != bfd_section_size (second))
ececd218 1044 return false;
94277a38 1045
818f79f6 1046 /* In-memory addresses may start at a different offset, relativize them. */
fd361982
AM
1047 if (bfd_section_vma (first) - bfd_get_start_address (first->owner)
1048 != bfd_section_vma (second) - bfd_get_start_address (second->owner))
ececd218 1049 return false;
94277a38 1050
fd361982
AM
1051 if (bfd_section_name (first) == NULL
1052 || bfd_section_name (second) == NULL
1053 || strcmp (bfd_section_name (first), bfd_section_name (second)) != 0)
ececd218 1054 return false;
94277a38
DJ
1055
1056 /* Otherwise check that they are in corresponding objfiles. */
1057
9d7c67bf 1058 struct objfile *obj = NULL;
2030c079 1059 for (objfile *objfile : current_program_space->objfiles ())
aed57c53
TT
1060 if (objfile->obfd == first->owner)
1061 {
1062 obj = objfile;
1063 break;
1064 }
94277a38
DJ
1065 gdb_assert (obj != NULL);
1066
1067 if (obj->separate_debug_objfile != NULL
1068 && obj->separate_debug_objfile->obfd == second->owner)
ececd218 1069 return true;
94277a38
DJ
1070 if (obj->separate_debug_objfile_backlink != NULL
1071 && obj->separate_debug_objfile_backlink->obfd == second->owner)
ececd218 1072 return true;
94277a38 1073
ececd218 1074 return false;
94277a38 1075}
c5aa993b 1076
2097ae25
DE
1077/* See symtab.h. */
1078
1079void
1080expand_symtab_containing_pc (CORE_ADDR pc, struct obj_section *section)
c906108c 1081{
77e371c0 1082 struct bound_minimal_symbol msymbol;
8a48e967
DJ
1083
1084 /* If we know that this is not a text address, return failure. This is
1085 necessary because we loop based on texthigh and textlow, which do
1086 not include the data ranges. */
77e371c0 1087 msymbol = lookup_minimal_symbol_by_pc_section (pc, section);
1ed9f74e 1088 if (msymbol.minsym && msymbol.minsym->data_p ())
2097ae25 1089 return;
c906108c 1090
2030c079 1091 for (objfile *objfile : current_program_space->objfiles ())
aed57c53
TT
1092 {
1093 struct compunit_symtab *cust = NULL;
433759f7 1094
aed57c53
TT
1095 if (objfile->sf)
1096 cust = objfile->sf->qf->find_pc_sect_compunit_symtab (objfile, msymbol,
1097 pc, section, 0);
1098 if (cust)
1099 return;
1100 }
c906108c 1101}
c906108c 1102\f
f57d2163
DE
1103/* Hash function for the symbol cache. */
1104
1105static unsigned int
1106hash_symbol_entry (const struct objfile *objfile_context,
1107 const char *name, domain_enum domain)
1108{
1109 unsigned int hash = (uintptr_t) objfile_context;
1110
1111 if (name != NULL)
1112 hash += htab_hash_string (name);
1113
2c26b84f
DE
1114 /* Because of symbol_matches_domain we need VAR_DOMAIN and STRUCT_DOMAIN
1115 to map to the same slot. */
1116 if (domain == STRUCT_DOMAIN)
1117 hash += VAR_DOMAIN * 7;
1118 else
1119 hash += domain * 7;
f57d2163
DE
1120
1121 return hash;
1122}
1123
1124/* Equality function for the symbol cache. */
1125
1126static int
1127eq_symbol_entry (const struct symbol_cache_slot *slot,
1128 const struct objfile *objfile_context,
1129 const char *name, domain_enum domain)
1130{
1131 const char *slot_name;
1132 domain_enum slot_domain;
1133
1134 if (slot->state == SYMBOL_SLOT_UNUSED)
1135 return 0;
1136
1137 if (slot->objfile_context != objfile_context)
1138 return 0;
1139
1140 if (slot->state == SYMBOL_SLOT_NOT_FOUND)
1141 {
1142 slot_name = slot->value.not_found.name;
1143 slot_domain = slot->value.not_found.domain;
1144 }
1145 else
1146 {
d12307c1
PMR
1147 slot_name = SYMBOL_SEARCH_NAME (slot->value.found.symbol);
1148 slot_domain = SYMBOL_DOMAIN (slot->value.found.symbol);
f57d2163
DE
1149 }
1150
1151 /* NULL names match. */
1152 if (slot_name == NULL && name == NULL)
1153 {
1154 /* But there's no point in calling symbol_matches_domain in the
1155 SYMBOL_SLOT_FOUND case. */
1156 if (slot_domain != domain)
1157 return 0;
1158 }
1159 else if (slot_name != NULL && name != NULL)
1160 {
b5ec771e
PA
1161 /* It's important that we use the same comparison that was done
1162 the first time through. If the slot records a found symbol,
1163 then this means using the symbol name comparison function of
1164 the symbol's language with SYMBOL_SEARCH_NAME. See
1165 dictionary.c. It also means using symbol_matches_domain for
1166 found symbols. See block.c.
f57d2163
DE
1167
1168 If the slot records a not-found symbol, then require a precise match.
1169 We could still be lax with whitespace like strcmp_iw though. */
1170
1171 if (slot->state == SYMBOL_SLOT_NOT_FOUND)
1172 {
1173 if (strcmp (slot_name, name) != 0)
1174 return 0;
1175 if (slot_domain != domain)
1176 return 0;
1177 }
1178 else
1179 {
d12307c1 1180 struct symbol *sym = slot->value.found.symbol;
b5ec771e 1181 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
f57d2163 1182
b5ec771e 1183 if (!SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
f57d2163 1184 return 0;
b5ec771e 1185
f57d2163
DE
1186 if (!symbol_matches_domain (SYMBOL_LANGUAGE (sym),
1187 slot_domain, domain))
1188 return 0;
1189 }
1190 }
1191 else
1192 {
1193 /* Only one name is NULL. */
1194 return 0;
1195 }
1196
1197 return 1;
1198}
1199
1200/* Given a cache of size SIZE, return the size of the struct (with variable
1201 length array) in bytes. */
1202
1203static size_t
1204symbol_cache_byte_size (unsigned int size)
1205{
1206 return (sizeof (struct block_symbol_cache)
1207 + ((size - 1) * sizeof (struct symbol_cache_slot)));
1208}
1209
1210/* Resize CACHE. */
1211
1212static void
1213resize_symbol_cache (struct symbol_cache *cache, unsigned int new_size)
1214{
1215 /* If there's no change in size, don't do anything.
1216 All caches have the same size, so we can just compare with the size
1217 of the global symbols cache. */
1218 if ((cache->global_symbols != NULL
1219 && cache->global_symbols->size == new_size)
1220 || (cache->global_symbols == NULL
1221 && new_size == 0))
1222 return;
1223
1224 xfree (cache->global_symbols);
1225 xfree (cache->static_symbols);
1226
1227 if (new_size == 0)
1228 {
1229 cache->global_symbols = NULL;
1230 cache->static_symbols = NULL;
1231 }
1232 else
1233 {
1234 size_t total_size = symbol_cache_byte_size (new_size);
1235
224c3ddb
SM
1236 cache->global_symbols
1237 = (struct block_symbol_cache *) xcalloc (1, total_size);
1238 cache->static_symbols
1239 = (struct block_symbol_cache *) xcalloc (1, total_size);
f57d2163
DE
1240 cache->global_symbols->size = new_size;
1241 cache->static_symbols->size = new_size;
1242 }
1243}
1244
f57d2163
DE
1245/* Return the symbol cache of PSPACE.
1246 Create one if it doesn't exist yet. */
1247
1248static struct symbol_cache *
1249get_symbol_cache (struct program_space *pspace)
1250{
3017b94d 1251 struct symbol_cache *cache = symbol_cache_key.get (pspace);
f57d2163
DE
1252
1253 if (cache == NULL)
1254 {
3017b94d
TT
1255 cache = symbol_cache_key.emplace (pspace);
1256 resize_symbol_cache (cache, symbol_cache_size);
f57d2163
DE
1257 }
1258
1259 return cache;
1260}
1261
f57d2163
DE
1262/* Set the size of the symbol cache in all program spaces. */
1263
1264static void
1265set_symbol_cache_size (unsigned int new_size)
1266{
1267 struct program_space *pspace;
1268
1269 ALL_PSPACES (pspace)
1270 {
3017b94d 1271 struct symbol_cache *cache = symbol_cache_key.get (pspace);
f57d2163
DE
1272
1273 /* The pspace could have been created but not have a cache yet. */
1274 if (cache != NULL)
1275 resize_symbol_cache (cache, new_size);
1276 }
1277}
1278
1279/* Called when symbol-cache-size is set. */
1280
1281static void
eb4c3f4a 1282set_symbol_cache_size_handler (const char *args, int from_tty,
f57d2163
DE
1283 struct cmd_list_element *c)
1284{
1285 if (new_symbol_cache_size > MAX_SYMBOL_CACHE_SIZE)
1286 {
1287 /* Restore the previous value.
1288 This is the value the "show" command prints. */
1289 new_symbol_cache_size = symbol_cache_size;
1290
1291 error (_("Symbol cache size is too large, max is %u."),
1292 MAX_SYMBOL_CACHE_SIZE);
1293 }
1294 symbol_cache_size = new_symbol_cache_size;
1295
1296 set_symbol_cache_size (symbol_cache_size);
1297}
1298
1299/* Lookup symbol NAME,DOMAIN in BLOCK in the symbol cache of PSPACE.
1300 OBJFILE_CONTEXT is the current objfile, which may be NULL.
1301 The result is the symbol if found, SYMBOL_LOOKUP_FAILED if a previous lookup
1302 failed (and thus this one will too), or NULL if the symbol is not present
1303 in the cache.
d0509ba4
CB
1304 *BSC_PTR and *SLOT_PTR are set to the cache and slot of the symbol, which
1305 can be used to save the result of a full lookup attempt. */
f57d2163 1306
d12307c1 1307static struct block_symbol
f57d2163 1308symbol_cache_lookup (struct symbol_cache *cache,
ddbcedf5 1309 struct objfile *objfile_context, enum block_enum block,
f57d2163
DE
1310 const char *name, domain_enum domain,
1311 struct block_symbol_cache **bsc_ptr,
1312 struct symbol_cache_slot **slot_ptr)
1313{
1314 struct block_symbol_cache *bsc;
1315 unsigned int hash;
1316 struct symbol_cache_slot *slot;
1317
1318 if (block == GLOBAL_BLOCK)
1319 bsc = cache->global_symbols;
1320 else
1321 bsc = cache->static_symbols;
1322 if (bsc == NULL)
1323 {
1324 *bsc_ptr = NULL;
1325 *slot_ptr = NULL;
6640a367 1326 return {};
f57d2163
DE
1327 }
1328
1329 hash = hash_symbol_entry (objfile_context, name, domain);
1330 slot = bsc->symbols + hash % bsc->size;
f57d2163 1331
d0509ba4
CB
1332 *bsc_ptr = bsc;
1333 *slot_ptr = slot;
1334
f57d2163
DE
1335 if (eq_symbol_entry (slot, objfile_context, name, domain))
1336 {
1337 if (symbol_lookup_debug)
1338 fprintf_unfiltered (gdb_stdlog,
1339 "%s block symbol cache hit%s for %s, %s\n",
1340 block == GLOBAL_BLOCK ? "Global" : "Static",
1341 slot->state == SYMBOL_SLOT_NOT_FOUND
1342 ? " (not found)" : "",
1343 name, domain_name (domain));
1344 ++bsc->hits;
1345 if (slot->state == SYMBOL_SLOT_NOT_FOUND)
1346 return SYMBOL_LOOKUP_FAILED;
1347 return slot->value.found;
1348 }
1349
2c26b84f
DE
1350 /* Symbol is not present in the cache. */
1351
f57d2163
DE
1352 if (symbol_lookup_debug)
1353 {
1354 fprintf_unfiltered (gdb_stdlog,
1355 "%s block symbol cache miss for %s, %s\n",
1356 block == GLOBAL_BLOCK ? "Global" : "Static",
1357 name, domain_name (domain));
1358 }
1359 ++bsc->misses;
6640a367 1360 return {};
f57d2163
DE
1361}
1362
1363/* Clear out SLOT. */
1364
1365static void
1366symbol_cache_clear_slot (struct symbol_cache_slot *slot)
1367{
1368 if (slot->state == SYMBOL_SLOT_NOT_FOUND)
1369 xfree (slot->value.not_found.name);
1370 slot->state = SYMBOL_SLOT_UNUSED;
1371}
1372
1373/* Mark SYMBOL as found in SLOT.
1374 OBJFILE_CONTEXT is the current objfile when the lookup was done, or NULL
1375 if it's not needed to distinguish lookups (STATIC_BLOCK). It is *not*
1376 necessarily the objfile the symbol was found in. */
1377
1378static void
1379symbol_cache_mark_found (struct block_symbol_cache *bsc,
1380 struct symbol_cache_slot *slot,
1381 struct objfile *objfile_context,
d12307c1
PMR
1382 struct symbol *symbol,
1383 const struct block *block)
f57d2163
DE
1384{
1385 if (bsc == NULL)
1386 return;
1387 if (slot->state != SYMBOL_SLOT_UNUSED)
1388 {
1389 ++bsc->collisions;
1390 symbol_cache_clear_slot (slot);
1391 }
1392 slot->state = SYMBOL_SLOT_FOUND;
1393 slot->objfile_context = objfile_context;
d12307c1
PMR
1394 slot->value.found.symbol = symbol;
1395 slot->value.found.block = block;
f57d2163
DE
1396}
1397
1398/* Mark symbol NAME, DOMAIN as not found in SLOT.
1399 OBJFILE_CONTEXT is the current objfile when the lookup was done, or NULL
1400 if it's not needed to distinguish lookups (STATIC_BLOCK). */
1401
1402static void
1403symbol_cache_mark_not_found (struct block_symbol_cache *bsc,
1404 struct symbol_cache_slot *slot,
1405 struct objfile *objfile_context,
1406 const char *name, domain_enum domain)
1407{
1408 if (bsc == NULL)
1409 return;
1410 if (slot->state != SYMBOL_SLOT_UNUSED)
1411 {
1412 ++bsc->collisions;
1413 symbol_cache_clear_slot (slot);
1414 }
1415 slot->state = SYMBOL_SLOT_NOT_FOUND;
1416 slot->objfile_context = objfile_context;
1417 slot->value.not_found.name = xstrdup (name);
1418 slot->value.not_found.domain = domain;
1419}
1420
1421/* Flush the symbol cache of PSPACE. */
1422
1423static void
1424symbol_cache_flush (struct program_space *pspace)
1425{
3017b94d 1426 struct symbol_cache *cache = symbol_cache_key.get (pspace);
f57d2163 1427 int pass;
f57d2163
DE
1428
1429 if (cache == NULL)
1430 return;
1431 if (cache->global_symbols == NULL)
1432 {
1433 gdb_assert (symbol_cache_size == 0);
1434 gdb_assert (cache->static_symbols == NULL);
1435 return;
1436 }
1437
1438 /* If the cache is untouched since the last flush, early exit.
1439 This is important for performance during the startup of a program linked
1440 with 100s (or 1000s) of shared libraries. */
1441 if (cache->global_symbols->misses == 0
1442 && cache->static_symbols->misses == 0)
1443 return;
1444
1445 gdb_assert (cache->global_symbols->size == symbol_cache_size);
1446 gdb_assert (cache->static_symbols->size == symbol_cache_size);
1447
1448 for (pass = 0; pass < 2; ++pass)
1449 {
1450 struct block_symbol_cache *bsc
1451 = pass == 0 ? cache->global_symbols : cache->static_symbols;
1452 unsigned int i;
1453
1454 for (i = 0; i < bsc->size; ++i)
1455 symbol_cache_clear_slot (&bsc->symbols[i]);
1456 }
1457
1458 cache->global_symbols->hits = 0;
1459 cache->global_symbols->misses = 0;
1460 cache->global_symbols->collisions = 0;
1461 cache->static_symbols->hits = 0;
1462 cache->static_symbols->misses = 0;
1463 cache->static_symbols->collisions = 0;
1464}
1465
1466/* Dump CACHE. */
1467
1468static void
1469symbol_cache_dump (const struct symbol_cache *cache)
1470{
1471 int pass;
1472
1473 if (cache->global_symbols == NULL)
1474 {
1475 printf_filtered (" <disabled>\n");
1476 return;
1477 }
1478
1479 for (pass = 0; pass < 2; ++pass)
1480 {
1481 const struct block_symbol_cache *bsc
1482 = pass == 0 ? cache->global_symbols : cache->static_symbols;
1483 unsigned int i;
1484
1485 if (pass == 0)
1486 printf_filtered ("Global symbols:\n");
1487 else
1488 printf_filtered ("Static symbols:\n");
1489
1490 for (i = 0; i < bsc->size; ++i)
1491 {
1492 const struct symbol_cache_slot *slot = &bsc->symbols[i];
1493
1494 QUIT;
1495
1496 switch (slot->state)
1497 {
1498 case SYMBOL_SLOT_UNUSED:
1499 break;
1500 case SYMBOL_SLOT_NOT_FOUND:
2c26b84f 1501 printf_filtered (" [%4u] = %s, %s %s (not found)\n", i,
f57d2163 1502 host_address_to_string (slot->objfile_context),
2c26b84f
DE
1503 slot->value.not_found.name,
1504 domain_name (slot->value.not_found.domain));
f57d2163
DE
1505 break;
1506 case SYMBOL_SLOT_FOUND:
d12307c1
PMR
1507 {
1508 struct symbol *found = slot->value.found.symbol;
1509 const struct objfile *context = slot->objfile_context;
1510
1511 printf_filtered (" [%4u] = %s, %s %s\n", i,
1512 host_address_to_string (context),
1513 SYMBOL_PRINT_NAME (found),
1514 domain_name (SYMBOL_DOMAIN (found)));
1515 break;
1516 }
f57d2163
DE
1517 }
1518 }
1519 }
1520}
1521
1522/* The "mt print symbol-cache" command. */
1523
1524static void
510e5e56 1525maintenance_print_symbol_cache (const char *args, int from_tty)
f57d2163
DE
1526{
1527 struct program_space *pspace;
1528
1529 ALL_PSPACES (pspace)
1530 {
1531 struct symbol_cache *cache;
1532
1533 printf_filtered (_("Symbol cache for pspace %d\n%s:\n"),
1534 pspace->num,
1535 pspace->symfile_object_file != NULL
1536 ? objfile_name (pspace->symfile_object_file)
1537 : "(no object file)");
1538
1539 /* If the cache hasn't been created yet, avoid creating one. */
3017b94d 1540 cache = symbol_cache_key.get (pspace);
f57d2163
DE
1541 if (cache == NULL)
1542 printf_filtered (" <empty>\n");
1543 else
1544 symbol_cache_dump (cache);
1545 }
1546}
1547
1548/* The "mt flush-symbol-cache" command. */
1549
1550static void
510e5e56 1551maintenance_flush_symbol_cache (const char *args, int from_tty)
f57d2163
DE
1552{
1553 struct program_space *pspace;
1554
1555 ALL_PSPACES (pspace)
1556 {
1557 symbol_cache_flush (pspace);
1558 }
1559}
1560
1561/* Print usage statistics of CACHE. */
1562
1563static void
1564symbol_cache_stats (struct symbol_cache *cache)
1565{
1566 int pass;
1567
1568 if (cache->global_symbols == NULL)
1569 {
1570 printf_filtered (" <disabled>\n");
1571 return;
1572 }
1573
1574 for (pass = 0; pass < 2; ++pass)
1575 {
1576 const struct block_symbol_cache *bsc
1577 = pass == 0 ? cache->global_symbols : cache->static_symbols;
1578
1579 QUIT;
1580
1581 if (pass == 0)
1582 printf_filtered ("Global block cache stats:\n");
1583 else
1584 printf_filtered ("Static block cache stats:\n");
1585
1586 printf_filtered (" size: %u\n", bsc->size);
1587 printf_filtered (" hits: %u\n", bsc->hits);
1588 printf_filtered (" misses: %u\n", bsc->misses);
1589 printf_filtered (" collisions: %u\n", bsc->collisions);
1590 }
1591}
1592
1593/* The "mt print symbol-cache-statistics" command. */
1594
1595static void
510e5e56 1596maintenance_print_symbol_cache_statistics (const char *args, int from_tty)
f57d2163
DE
1597{
1598 struct program_space *pspace;
1599
1600 ALL_PSPACES (pspace)
1601 {
1602 struct symbol_cache *cache;
1603
1604 printf_filtered (_("Symbol cache statistics for pspace %d\n%s:\n"),
1605 pspace->num,
1606 pspace->symfile_object_file != NULL
1607 ? objfile_name (pspace->symfile_object_file)
1608 : "(no object file)");
1609
1610 /* If the cache hasn't been created yet, avoid creating one. */
3017b94d 1611 cache = symbol_cache_key.get (pspace);
f57d2163
DE
1612 if (cache == NULL)
1613 printf_filtered (" empty, no stats available\n");
1614 else
1615 symbol_cache_stats (cache);
1616 }
1617}
1618
1619/* This module's 'new_objfile' observer. */
1620
1621static void
1622symtab_new_objfile_observer (struct objfile *objfile)
1623{
1624 /* Ideally we'd use OBJFILE->pspace, but OBJFILE may be NULL. */
1625 symbol_cache_flush (current_program_space);
1626}
1627
1628/* This module's 'free_objfile' observer. */
1629
1630static void
1631symtab_free_objfile_observer (struct objfile *objfile)
1632{
1633 symbol_cache_flush (objfile->pspace);
1634}
1635\f
c906108c
SS
1636/* Debug symbols usually don't have section information. We need to dig that
1637 out of the minimal symbols and stash that in the debug symbol. */
1638
ccefe4c4 1639void
907fc202
UW
1640fixup_section (struct general_symbol_info *ginfo,
1641 CORE_ADDR addr, struct objfile *objfile)
c906108c
SS
1642{
1643 struct minimal_symbol *msym;
c906108c 1644
bccdca4a
UW
1645 /* First, check whether a minimal symbol with the same name exists
1646 and points to the same address. The address check is required
1647 e.g. on PowerPC64, where the minimal symbol for a function will
1648 point to the function descriptor, while the debug symbol will
1649 point to the actual function code. */
907fc202
UW
1650 msym = lookup_minimal_symbol_by_pc_name (addr, ginfo->name, objfile);
1651 if (msym)
efd66ac6 1652 ginfo->section = MSYMBOL_SECTION (msym);
907fc202 1653 else
19e2d14b
KB
1654 {
1655 /* Static, function-local variables do appear in the linker
1656 (minimal) symbols, but are frequently given names that won't
1657 be found via lookup_minimal_symbol(). E.g., it has been
1658 observed in frv-uclinux (ELF) executables that a static,
1659 function-local variable named "foo" might appear in the
1660 linker symbols as "foo.6" or "foo.3". Thus, there is no
1661 point in attempting to extend the lookup-by-name mechanism to
1662 handle this case due to the fact that there can be multiple
1663 names.
9af17804 1664
19e2d14b
KB
1665 So, instead, search the section table when lookup by name has
1666 failed. The ``addr'' and ``endaddr'' fields may have already
1667 been relocated. If so, the relocation offset (i.e. the
1668 ANOFFSET value) needs to be subtracted from these values when
1669 performing the comparison. We unconditionally subtract it,
1670 because, when no relocation has been performed, the ANOFFSET
1671 value will simply be zero.
9af17804 1672
19e2d14b
KB
1673 The address of the symbol whose section we're fixing up HAS
1674 NOT BEEN adjusted (relocated) yet. It can't have been since
1675 the section isn't yet known and knowing the section is
1676 necessary in order to add the correct relocation value. In
1677 other words, we wouldn't even be in this function (attempting
1678 to compute the section) if it were already known.
1679
1680 Note that it is possible to search the minimal symbols
1681 (subtracting the relocation value if necessary) to find the
1682 matching minimal symbol, but this is overkill and much less
1683 efficient. It is not necessary to find the matching minimal
9af17804
DE
1684 symbol, only its section.
1685
19e2d14b
KB
1686 Note that this technique (of doing a section table search)
1687 can fail when unrelocated section addresses overlap. For
1688 this reason, we still attempt a lookup by name prior to doing
1689 a search of the section table. */
9af17804 1690
19e2d14b 1691 struct obj_section *s;
e27d198c 1692 int fallback = -1;
433759f7 1693
19e2d14b
KB
1694 ALL_OBJFILE_OSECTIONS (objfile, s)
1695 {
65cf3563 1696 int idx = s - objfile->sections;
19e2d14b
KB
1697 CORE_ADDR offset = ANOFFSET (objfile->section_offsets, idx);
1698
e27d198c
TT
1699 if (fallback == -1)
1700 fallback = idx;
1701
f1f6aadf
PA
1702 if (obj_section_addr (s) - offset <= addr
1703 && addr < obj_section_endaddr (s) - offset)
19e2d14b 1704 {
19e2d14b
KB
1705 ginfo->section = idx;
1706 return;
1707 }
1708 }
e27d198c
TT
1709
1710 /* If we didn't find the section, assume it is in the first
1711 section. If there is no allocated section, then it hardly
1712 matters what we pick, so just pick zero. */
1713 if (fallback == -1)
1714 ginfo->section = 0;
1715 else
1716 ginfo->section = fallback;
19e2d14b 1717 }
c906108c
SS
1718}
1719
1720struct symbol *
fba45db2 1721fixup_symbol_section (struct symbol *sym, struct objfile *objfile)
c906108c 1722{
907fc202
UW
1723 CORE_ADDR addr;
1724
c906108c
SS
1725 if (!sym)
1726 return NULL;
1727
1994afbf
DE
1728 if (!SYMBOL_OBJFILE_OWNED (sym))
1729 return sym;
1730
907fc202
UW
1731 /* We either have an OBJFILE, or we can get at it from the sym's
1732 symtab. Anything else is a bug. */
08be3fe3 1733 gdb_assert (objfile || symbol_symtab (sym));
907fc202
UW
1734
1735 if (objfile == NULL)
08be3fe3 1736 objfile = symbol_objfile (sym);
907fc202 1737
e27d198c
TT
1738 if (SYMBOL_OBJ_SECTION (objfile, sym))
1739 return sym;
1740
907fc202
UW
1741 /* We should have an objfile by now. */
1742 gdb_assert (objfile);
1743
1744 switch (SYMBOL_CLASS (sym))
1745 {
1746 case LOC_STATIC:
1747 case LOC_LABEL:
907fc202
UW
1748 addr = SYMBOL_VALUE_ADDRESS (sym);
1749 break;
1750 case LOC_BLOCK:
2b1ffcfd 1751 addr = BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym));
907fc202
UW
1752 break;
1753
1754 default:
1755 /* Nothing else will be listed in the minsyms -- no use looking
1756 it up. */
1757 return sym;
1758 }
1759
1760 fixup_section (&sym->ginfo, addr, objfile);
c906108c
SS
1761
1762 return sym;
1763}
1764
b5ec771e
PA
1765/* See symtab.h. */
1766
1767demangle_for_lookup_info::demangle_for_lookup_info
1768 (const lookup_name_info &lookup_name, language lang)
1769{
1770 demangle_result_storage storage;
1771
c62446b1
PA
1772 if (lookup_name.ignore_parameters () && lang == language_cplus)
1773 {
1774 gdb::unique_xmalloc_ptr<char> without_params
1775 = cp_remove_params_if_any (lookup_name.name ().c_str (),
1776 lookup_name.completion_mode ());
1777
1778 if (without_params != NULL)
1779 {
de63c46b
PA
1780 if (lookup_name.match_type () != symbol_name_match_type::SEARCH_NAME)
1781 m_demangled_name = demangle_for_lookup (without_params.get (),
1782 lang, storage);
c62446b1
PA
1783 return;
1784 }
1785 }
1786
de63c46b
PA
1787 if (lookup_name.match_type () == symbol_name_match_type::SEARCH_NAME)
1788 m_demangled_name = lookup_name.name ();
1789 else
1790 m_demangled_name = demangle_for_lookup (lookup_name.name ().c_str (),
1791 lang, storage);
b5ec771e
PA
1792}
1793
1794/* See symtab.h. */
1795
1796const lookup_name_info &
1797lookup_name_info::match_any ()
1798{
1799 /* Lookup any symbol that "" would complete. I.e., this matches all
1800 symbol names. */
1801 static const lookup_name_info lookup_name ({}, symbol_name_match_type::FULL,
1802 true);
1803
1804 return lookup_name;
1805}
1806
f8eba3c6 1807/* Compute the demangled form of NAME as used by the various symbol
2f408ecb
PA
1808 lookup functions. The result can either be the input NAME
1809 directly, or a pointer to a buffer owned by the STORAGE object.
f8eba3c6 1810
2f408ecb 1811 For Ada, this function just returns NAME, unmodified.
f8eba3c6
TT
1812 Normally, Ada symbol lookups are performed using the encoded name
1813 rather than the demangled name, and so it might seem to make sense
1814 for this function to return an encoded version of NAME.
1815 Unfortunately, we cannot do this, because this function is used in
1816 circumstances where it is not appropriate to try to encode NAME.
1817 For instance, when displaying the frame info, we demangle the name
1818 of each parameter, and then perform a symbol lookup inside our
1819 function using that demangled name. In Ada, certain functions
1820 have internally-generated parameters whose name contain uppercase
1821 characters. Encoding those name would result in those uppercase
1822 characters to become lowercase, and thus cause the symbol lookup
1823 to fail. */
c906108c 1824
2f408ecb 1825const char *
f8eba3c6 1826demangle_for_lookup (const char *name, enum language lang,
2f408ecb 1827 demangle_result_storage &storage)
c906108c 1828{
9c37b5ae 1829 /* If we are using C++, D, or Go, demangle the name before doing a
c378eb4e 1830 lookup, so we can always binary search. */
53c5240f 1831 if (lang == language_cplus)
729051e6 1832 {
2f408ecb
PA
1833 char *demangled_name = gdb_demangle (name, DMGL_ANSI | DMGL_PARAMS);
1834 if (demangled_name != NULL)
1835 return storage.set_malloc_ptr (demangled_name);
1836
1837 /* If we were given a non-mangled name, canonicalize it
1838 according to the language (so far only for C++). */
1839 std::string canon = cp_canonicalize_string (name);
1840 if (!canon.empty ())
1841 return storage.swap_string (canon);
729051e6 1842 }
6aecb9c2
JB
1843 else if (lang == language_d)
1844 {
2f408ecb
PA
1845 char *demangled_name = d_demangle (name, 0);
1846 if (demangled_name != NULL)
1847 return storage.set_malloc_ptr (demangled_name);
6aecb9c2 1848 }
a766d390
DE
1849 else if (lang == language_go)
1850 {
2f408ecb
PA
1851 char *demangled_name = go_demangle (name, 0);
1852 if (demangled_name != NULL)
1853 return storage.set_malloc_ptr (demangled_name);
a766d390 1854 }
729051e6 1855
2f408ecb 1856 return name;
f8eba3c6
TT
1857}
1858
5ffa0793
PA
1859/* See symtab.h. */
1860
1861unsigned int
1862search_name_hash (enum language language, const char *search_name)
1863{
1864 return language_def (language)->la_search_name_hash (search_name);
1865}
1866
cf901d3b 1867/* See symtab.h.
f8eba3c6 1868
cf901d3b 1869 This function (or rather its subordinates) have a bunch of loops and
7e082072
DE
1870 it would seem to be attractive to put in some QUIT's (though I'm not really
1871 sure whether it can run long enough to be really important). But there
f8eba3c6 1872 are a few calls for which it would appear to be bad news to quit
7e082072 1873 out of here: e.g., find_proc_desc in alpha-mdebug-tdep.c. (Note
f8eba3c6
TT
1874 that there is C++ code below which can error(), but that probably
1875 doesn't affect these calls since they are looking for a known
1876 variable and thus can probably assume it will never hit the C++
1877 code). */
1878
d12307c1 1879struct block_symbol
f8eba3c6
TT
1880lookup_symbol_in_language (const char *name, const struct block *block,
1881 const domain_enum domain, enum language lang,
1993b719 1882 struct field_of_this_result *is_a_field_of_this)
f8eba3c6 1883{
2f408ecb
PA
1884 demangle_result_storage storage;
1885 const char *modified_name = demangle_for_lookup (name, lang, storage);
f8eba3c6 1886
de63c46b
PA
1887 return lookup_symbol_aux (modified_name,
1888 symbol_name_match_type::FULL,
1889 block, domain, lang,
2f408ecb 1890 is_a_field_of_this);
fba7f19c
EZ
1891}
1892
cf901d3b 1893/* See symtab.h. */
53c5240f 1894
d12307c1 1895struct block_symbol
53c5240f 1896lookup_symbol (const char *name, const struct block *block,
1993b719
TT
1897 domain_enum domain,
1898 struct field_of_this_result *is_a_field_of_this)
53c5240f
PA
1899{
1900 return lookup_symbol_in_language (name, block, domain,
1901 current_language->la_language,
2570f2b7 1902 is_a_field_of_this);
53c5240f
PA
1903}
1904
cf901d3b 1905/* See symtab.h. */
66a17cb6 1906
de63c46b
PA
1907struct block_symbol
1908lookup_symbol_search_name (const char *search_name, const struct block *block,
1909 domain_enum domain)
1910{
1911 return lookup_symbol_aux (search_name, symbol_name_match_type::SEARCH_NAME,
1912 block, domain, language_asm, NULL);
1913}
1914
1915/* See symtab.h. */
1916
d12307c1 1917struct block_symbol
66a17cb6
TT
1918lookup_language_this (const struct language_defn *lang,
1919 const struct block *block)
1920{
1921 if (lang->la_name_of_this == NULL || block == NULL)
6640a367 1922 return {};
66a17cb6 1923
cc485e62
DE
1924 if (symbol_lookup_debug > 1)
1925 {
1926 struct objfile *objfile = lookup_objfile_from_block (block);
1927
1928 fprintf_unfiltered (gdb_stdlog,
1929 "lookup_language_this (%s, %s (objfile %s))",
1930 lang->la_name, host_address_to_string (block),
1931 objfile_debug_name (objfile));
1932 }
1933
03de6823 1934 while (block)
66a17cb6
TT
1935 {
1936 struct symbol *sym;
1937
de63c46b
PA
1938 sym = block_lookup_symbol (block, lang->la_name_of_this,
1939 symbol_name_match_type::SEARCH_NAME,
1940 VAR_DOMAIN);
66a17cb6 1941 if (sym != NULL)
f149aabd 1942 {
cc485e62
DE
1943 if (symbol_lookup_debug > 1)
1944 {
1945 fprintf_unfiltered (gdb_stdlog, " = %s (%s, block %s)\n",
1946 SYMBOL_PRINT_NAME (sym),
1947 host_address_to_string (sym),
1948 host_address_to_string (block));
1949 }
d12307c1 1950 return (struct block_symbol) {sym, block};
f149aabd 1951 }
66a17cb6 1952 if (BLOCK_FUNCTION (block))
03de6823 1953 break;
66a17cb6
TT
1954 block = BLOCK_SUPERBLOCK (block);
1955 }
03de6823 1956
cc485e62
DE
1957 if (symbol_lookup_debug > 1)
1958 fprintf_unfiltered (gdb_stdlog, " = NULL\n");
6640a367 1959 return {};
66a17cb6
TT
1960}
1961
2dc3df72
TT
1962/* Given TYPE, a structure/union,
1963 return 1 if the component named NAME from the ultimate target
1964 structure/union is defined, otherwise, return 0. */
1965
1966static int
1993b719
TT
1967check_field (struct type *type, const char *name,
1968 struct field_of_this_result *is_a_field_of_this)
2dc3df72
TT
1969{
1970 int i;
1971
1972 /* The type may be a stub. */
f168693b 1973 type = check_typedef (type);
2dc3df72
TT
1974
1975 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1976 {
1977 const char *t_field_name = TYPE_FIELD_NAME (type, i);
1978
1979 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1993b719
TT
1980 {
1981 is_a_field_of_this->type = type;
1982 is_a_field_of_this->field = &TYPE_FIELD (type, i);
1983 return 1;
1984 }
2dc3df72
TT
1985 }
1986
1987 /* C++: If it was not found as a data field, then try to return it
1988 as a pointer to a method. */
1989
1990 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
1991 {
1992 if (strcmp_iw (TYPE_FN_FIELDLIST_NAME (type, i), name) == 0)
1993b719
TT
1993 {
1994 is_a_field_of_this->type = type;
1995 is_a_field_of_this->fn_field = &TYPE_FN_FIELDLIST (type, i);
1996 return 1;
1997 }
2dc3df72
TT
1998 }
1999
2000 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1993b719 2001 if (check_field (TYPE_BASECLASS (type, i), name, is_a_field_of_this))
2dc3df72
TT
2002 return 1;
2003
2004 return 0;
2005}
2006
53c5240f 2007/* Behave like lookup_symbol except that NAME is the natural name
7e082072 2008 (e.g., demangled name) of the symbol that we're looking for. */
5ad1c190 2009
d12307c1 2010static struct block_symbol
de63c46b
PA
2011lookup_symbol_aux (const char *name, symbol_name_match_type match_type,
2012 const struct block *block,
94af9270 2013 const domain_enum domain, enum language language,
1993b719 2014 struct field_of_this_result *is_a_field_of_this)
fba7f19c 2015{
d12307c1 2016 struct block_symbol result;
53c5240f 2017 const struct language_defn *langdef;
406bc4de 2018
cc485e62
DE
2019 if (symbol_lookup_debug)
2020 {
2021 struct objfile *objfile = lookup_objfile_from_block (block);
2022
2023 fprintf_unfiltered (gdb_stdlog,
2024 "lookup_symbol_aux (%s, %s (objfile %s), %s, %s)\n",
2025 name, host_address_to_string (block),
2026 objfile != NULL
2027 ? objfile_debug_name (objfile) : "NULL",
2028 domain_name (domain), language_str (language));
2029 }
2030
9a146a11
EZ
2031 /* Make sure we do something sensible with is_a_field_of_this, since
2032 the callers that set this parameter to some non-null value will
1993b719
TT
2033 certainly use it later. If we don't set it, the contents of
2034 is_a_field_of_this are undefined. */
9a146a11 2035 if (is_a_field_of_this != NULL)
1993b719 2036 memset (is_a_field_of_this, 0, sizeof (*is_a_field_of_this));
9a146a11 2037
e4051eeb
DC
2038 /* Search specified block and its superiors. Don't search
2039 STATIC_BLOCK or GLOBAL_BLOCK. */
c906108c 2040
de63c46b 2041 result = lookup_local_symbol (name, match_type, block, domain, language);
d12307c1 2042 if (result.symbol != NULL)
cc485e62
DE
2043 {
2044 if (symbol_lookup_debug)
2045 {
2046 fprintf_unfiltered (gdb_stdlog, "lookup_symbol_aux (...) = %s\n",
d12307c1 2047 host_address_to_string (result.symbol));
cc485e62 2048 }
d12307c1 2049 return result;
cc485e62 2050 }
c906108c 2051
53c5240f 2052 /* If requested to do so by the caller and if appropriate for LANGUAGE,
13387711 2053 check to see if NAME is a field of `this'. */
53c5240f
PA
2054
2055 langdef = language_def (language);
5f9a71c3 2056
6592e36f
TT
2057 /* Don't do this check if we are searching for a struct. It will
2058 not be found by check_field, but will be found by other
2059 means. */
2060 if (is_a_field_of_this != NULL && domain != STRUCT_DOMAIN)
c906108c 2061 {
d12307c1 2062 result = lookup_language_this (langdef, block);
2b2d9e11 2063
d12307c1 2064 if (result.symbol)
c906108c 2065 {
d12307c1 2066 struct type *t = result.symbol->type;
9af17804 2067
2b2d9e11
VP
2068 /* I'm not really sure that type of this can ever
2069 be typedefed; just be safe. */
f168693b 2070 t = check_typedef (t);
aa006118 2071 if (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_IS_REFERENCE (t))
2b2d9e11 2072 t = TYPE_TARGET_TYPE (t);
9af17804 2073
2b2d9e11
VP
2074 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
2075 && TYPE_CODE (t) != TYPE_CODE_UNION)
9af17804 2076 error (_("Internal error: `%s' is not an aggregate"),
2b2d9e11 2077 langdef->la_name_of_this);
9af17804 2078
1993b719 2079 if (check_field (t, name, is_a_field_of_this))
cc485e62
DE
2080 {
2081 if (symbol_lookup_debug)
2082 {
2083 fprintf_unfiltered (gdb_stdlog,
2084 "lookup_symbol_aux (...) = NULL\n");
2085 }
6640a367 2086 return {};
cc485e62 2087 }
c906108c
SS
2088 }
2089 }
2090
53c5240f 2091 /* Now do whatever is appropriate for LANGUAGE to look
774b6a14 2092 up static and global variables. */
c906108c 2093
d12307c1
PMR
2094 result = langdef->la_lookup_symbol_nonlocal (langdef, name, block, domain);
2095 if (result.symbol != NULL)
cc485e62
DE
2096 {
2097 if (symbol_lookup_debug)
2098 {
2099 fprintf_unfiltered (gdb_stdlog, "lookup_symbol_aux (...) = %s\n",
d12307c1 2100 host_address_to_string (result.symbol));
cc485e62 2101 }
d12307c1 2102 return result;
cc485e62 2103 }
c906108c 2104
774b6a14
TT
2105 /* Now search all static file-level symbols. Not strictly correct,
2106 but more useful than an error. */
41f62f39 2107
d12307c1 2108 result = lookup_static_symbol (name, domain);
cc485e62
DE
2109 if (symbol_lookup_debug)
2110 {
2111 fprintf_unfiltered (gdb_stdlog, "lookup_symbol_aux (...) = %s\n",
d12307c1
PMR
2112 result.symbol != NULL
2113 ? host_address_to_string (result.symbol)
2114 : "NULL");
cc485e62 2115 }
d12307c1 2116 return result;
41f62f39
JK
2117}
2118
e4051eeb 2119/* Check to see if the symbol is defined in BLOCK or its superiors.
89a9d1b1 2120 Don't search STATIC_BLOCK or GLOBAL_BLOCK. */
8155455b 2121
d12307c1 2122static struct block_symbol
de63c46b
PA
2123lookup_local_symbol (const char *name,
2124 symbol_name_match_type match_type,
2125 const struct block *block,
74016e12
DE
2126 const domain_enum domain,
2127 enum language language)
8155455b
DC
2128{
2129 struct symbol *sym;
89a9d1b1 2130 const struct block *static_block = block_static_block (block);
13387711
SW
2131 const char *scope = block_scope (block);
2132
e4051eeb
DC
2133 /* Check if either no block is specified or it's a global block. */
2134
89a9d1b1 2135 if (static_block == NULL)
6640a367 2136 return {};
e4051eeb 2137
89a9d1b1 2138 while (block != static_block)
f61e8913 2139 {
de63c46b 2140 sym = lookup_symbol_in_block (name, match_type, block, domain);
f61e8913 2141 if (sym != NULL)
d12307c1 2142 return (struct block_symbol) {sym, block};
edb3359d 2143
f55ee35c 2144 if (language == language_cplus || language == language_fortran)
13387711 2145 {
b926417a 2146 struct block_symbol blocksym
d12307c1
PMR
2147 = cp_lookup_symbol_imports_or_template (scope, name, block,
2148 domain);
2149
b926417a
TT
2150 if (blocksym.symbol != NULL)
2151 return blocksym;
13387711
SW
2152 }
2153
edb3359d
DJ
2154 if (BLOCK_FUNCTION (block) != NULL && block_inlined_p (block))
2155 break;
f61e8913
DC
2156 block = BLOCK_SUPERBLOCK (block);
2157 }
2158
3aee438b 2159 /* We've reached the end of the function without finding a result. */
e4051eeb 2160
6640a367 2161 return {};
f61e8913
DC
2162}
2163
cf901d3b 2164/* See symtab.h. */
3a40aaa0 2165
c0201579 2166struct objfile *
3a40aaa0
UW
2167lookup_objfile_from_block (const struct block *block)
2168{
3a40aaa0
UW
2169 if (block == NULL)
2170 return NULL;
2171
2172 block = block_global_block (block);
43f3e411 2173 /* Look through all blockvectors. */
2030c079 2174 for (objfile *obj : current_program_space->objfiles ())
d8aeb77f 2175 {
b669c953 2176 for (compunit_symtab *cust : obj->compunits ())
d8aeb77f
TT
2177 if (block == BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust),
2178 GLOBAL_BLOCK))
2179 {
2180 if (obj->separate_debug_objfile_backlink)
2181 obj = obj->separate_debug_objfile_backlink;
61f0d762 2182
d8aeb77f
TT
2183 return obj;
2184 }
2185 }
3a40aaa0
UW
2186
2187 return NULL;
2188}
2189
cf901d3b 2190/* See symtab.h. */
f61e8913 2191
5f9a71c3 2192struct symbol *
de63c46b
PA
2193lookup_symbol_in_block (const char *name, symbol_name_match_type match_type,
2194 const struct block *block,
d1a2d36d 2195 const domain_enum domain)
f61e8913
DC
2196{
2197 struct symbol *sym;
f61e8913 2198
cc485e62
DE
2199 if (symbol_lookup_debug > 1)
2200 {
2201 struct objfile *objfile = lookup_objfile_from_block (block);
2202
2203 fprintf_unfiltered (gdb_stdlog,
2204 "lookup_symbol_in_block (%s, %s (objfile %s), %s)",
2205 name, host_address_to_string (block),
2206 objfile_debug_name (objfile),
2207 domain_name (domain));
2208 }
2209
de63c46b 2210 sym = block_lookup_symbol (block, name, match_type, domain);
f61e8913 2211 if (sym)
8155455b 2212 {
cc485e62
DE
2213 if (symbol_lookup_debug > 1)
2214 {
2215 fprintf_unfiltered (gdb_stdlog, " = %s\n",
2216 host_address_to_string (sym));
2217 }
21b556f4 2218 return fixup_symbol_section (sym, NULL);
8155455b
DC
2219 }
2220
cc485e62
DE
2221 if (symbol_lookup_debug > 1)
2222 fprintf_unfiltered (gdb_stdlog, " = NULL\n");
8155455b
DC
2223 return NULL;
2224}
2225
cf901d3b 2226/* See symtab.h. */
3a40aaa0 2227
d12307c1 2228struct block_symbol
efad9b6a 2229lookup_global_symbol_from_objfile (struct objfile *main_objfile,
442853af 2230 enum block_enum block_index,
3a40aaa0 2231 const char *name,
21b556f4 2232 const domain_enum domain)
3a40aaa0 2233{
442853af
CB
2234 gdb_assert (block_index == GLOBAL_BLOCK || block_index == STATIC_BLOCK);
2235
bde09ab7 2236 for (objfile *objfile : main_objfile->separate_debug_objfiles ())
15d123c9 2237 {
d12307c1 2238 struct block_symbol result
442853af 2239 = lookup_symbol_in_objfile (objfile, block_index, name, domain);
15d123c9 2240
442853af 2241 if (result.symbol != nullptr)
d12307c1 2242 return result;
15d123c9 2243 }
56e3f43c 2244
6640a367 2245 return {};
3a40aaa0
UW
2246}
2247
19630284
JB
2248/* Check to see if the symbol is defined in one of the OBJFILE's
2249 symtabs. BLOCK_INDEX should be either GLOBAL_BLOCK or STATIC_BLOCK,
8155455b
DC
2250 depending on whether or not we want to search global symbols or
2251 static symbols. */
2252
d12307c1 2253static struct block_symbol
c32e6a04
CB
2254lookup_symbol_in_objfile_symtabs (struct objfile *objfile,
2255 enum block_enum block_index, const char *name,
2256 const domain_enum domain)
19630284 2257{
ba715d7f
JK
2258 gdb_assert (block_index == GLOBAL_BLOCK || block_index == STATIC_BLOCK);
2259
cc485e62
DE
2260 if (symbol_lookup_debug > 1)
2261 {
2262 fprintf_unfiltered (gdb_stdlog,
2263 "lookup_symbol_in_objfile_symtabs (%s, %s, %s, %s)",
2264 objfile_debug_name (objfile),
2265 block_index == GLOBAL_BLOCK
2266 ? "GLOBAL_BLOCK" : "STATIC_BLOCK",
2267 name, domain_name (domain));
2268 }
2269
b669c953 2270 for (compunit_symtab *cust : objfile->compunits ())
a743abeb 2271 {
43f3e411
DE
2272 const struct blockvector *bv;
2273 const struct block *block;
d12307c1 2274 struct block_symbol result;
43f3e411
DE
2275
2276 bv = COMPUNIT_BLOCKVECTOR (cust);
a743abeb 2277 block = BLOCKVECTOR_BLOCK (bv, block_index);
d12307c1
PMR
2278 result.symbol = block_lookup_symbol_primary (block, name, domain);
2279 result.block = block;
2280 if (result.symbol != NULL)
a743abeb 2281 {
cc485e62
DE
2282 if (symbol_lookup_debug > 1)
2283 {
2284 fprintf_unfiltered (gdb_stdlog, " = %s (block %s)\n",
d12307c1 2285 host_address_to_string (result.symbol),
cc485e62
DE
2286 host_address_to_string (block));
2287 }
d12307c1
PMR
2288 result.symbol = fixup_symbol_section (result.symbol, objfile);
2289 return result;
2290
a743abeb
DE
2291 }
2292 }
19630284 2293
cc485e62
DE
2294 if (symbol_lookup_debug > 1)
2295 fprintf_unfiltered (gdb_stdlog, " = NULL\n");
6640a367 2296 return {};
19630284
JB
2297}
2298
74016e12 2299/* Wrapper around lookup_symbol_in_objfile_symtabs for search_symbols.
422d65e7 2300 Look up LINKAGE_NAME in DOMAIN in the global and static blocks of OBJFILE
01465b56
DE
2301 and all associated separate debug objfiles.
2302
2303 Normally we only look in OBJFILE, and not any separate debug objfiles
2304 because the outer loop will cause them to be searched too. This case is
2305 different. Here we're called from search_symbols where it will only
6471e7d2 2306 call us for the objfile that contains a matching minsym. */
422d65e7 2307
d12307c1 2308static struct block_symbol
422d65e7
DE
2309lookup_symbol_in_objfile_from_linkage_name (struct objfile *objfile,
2310 const char *linkage_name,
2311 domain_enum domain)
2312{
2313 enum language lang = current_language->la_language;
e9ad22ee 2314 struct objfile *main_objfile;
422d65e7 2315
2f408ecb
PA
2316 demangle_result_storage storage;
2317 const char *modified_name = demangle_for_lookup (linkage_name, lang, storage);
2318
422d65e7
DE
2319 if (objfile->separate_debug_objfile_backlink)
2320 main_objfile = objfile->separate_debug_objfile_backlink;
2321 else
2322 main_objfile = objfile;
2323
bde09ab7 2324 for (::objfile *cur_objfile : main_objfile->separate_debug_objfiles ())
422d65e7 2325 {
d12307c1
PMR
2326 struct block_symbol result;
2327
2328 result = lookup_symbol_in_objfile_symtabs (cur_objfile, GLOBAL_BLOCK,
2329 modified_name, domain);
2330 if (result.symbol == NULL)
2331 result = lookup_symbol_in_objfile_symtabs (cur_objfile, STATIC_BLOCK,
2332 modified_name, domain);
2333 if (result.symbol != NULL)
2f408ecb 2334 return result;
422d65e7
DE
2335 }
2336
6640a367 2337 return {};
422d65e7
DE
2338}
2339
08c23b0d
TT
2340/* A helper function that throws an exception when a symbol was found
2341 in a psymtab but not in a symtab. */
2342
2343static void ATTRIBUTE_NORETURN
ddbcedf5 2344error_in_psymtab_expansion (enum block_enum block_index, const char *name,
43f3e411 2345 struct compunit_symtab *cust)
08c23b0d
TT
2346{
2347 error (_("\
2348Internal: %s symbol `%s' found in %s psymtab but not in symtab.\n\
2349%s may be an inlined function, or may be a template function\n \
2350(if a template, try specifying an instantiation: %s<type>)."),
f88cb4b6 2351 block_index == GLOBAL_BLOCK ? "global" : "static",
43f3e411
DE
2352 name,
2353 symtab_to_filename_for_display (compunit_primary_filetab (cust)),
2354 name, name);
08c23b0d
TT
2355}
2356
74016e12
DE
2357/* A helper function for various lookup routines that interfaces with
2358 the "quick" symbol table functions. */
8155455b 2359
d12307c1 2360static struct block_symbol
ddbcedf5
CB
2361lookup_symbol_via_quick_fns (struct objfile *objfile,
2362 enum block_enum block_index, const char *name,
2363 const domain_enum domain)
8155455b 2364{
43f3e411 2365 struct compunit_symtab *cust;
346d1dfe 2366 const struct blockvector *bv;
8155455b 2367 const struct block *block;
d12307c1 2368 struct block_symbol result;
8155455b 2369
ccefe4c4 2370 if (!objfile->sf)
6640a367 2371 return {};
cc485e62
DE
2372
2373 if (symbol_lookup_debug > 1)
2374 {
2375 fprintf_unfiltered (gdb_stdlog,
2376 "lookup_symbol_via_quick_fns (%s, %s, %s, %s)\n",
2377 objfile_debug_name (objfile),
2378 block_index == GLOBAL_BLOCK
2379 ? "GLOBAL_BLOCK" : "STATIC_BLOCK",
2380 name, domain_name (domain));
2381 }
2382
43f3e411
DE
2383 cust = objfile->sf->qf->lookup_symbol (objfile, block_index, name, domain);
2384 if (cust == NULL)
cc485e62
DE
2385 {
2386 if (symbol_lookup_debug > 1)
2387 {
2388 fprintf_unfiltered (gdb_stdlog,
2389 "lookup_symbol_via_quick_fns (...) = NULL\n");
2390 }
6640a367 2391 return {};
cc485e62 2392 }
8155455b 2393
43f3e411 2394 bv = COMPUNIT_BLOCKVECTOR (cust);
f88cb4b6 2395 block = BLOCKVECTOR_BLOCK (bv, block_index);
de63c46b
PA
2396 result.symbol = block_lookup_symbol (block, name,
2397 symbol_name_match_type::FULL, domain);
d12307c1 2398 if (result.symbol == NULL)
43f3e411 2399 error_in_psymtab_expansion (block_index, name, cust);
cc485e62
DE
2400
2401 if (symbol_lookup_debug > 1)
2402 {
2403 fprintf_unfiltered (gdb_stdlog,
2404 "lookup_symbol_via_quick_fns (...) = %s (block %s)\n",
d12307c1 2405 host_address_to_string (result.symbol),
cc485e62
DE
2406 host_address_to_string (block));
2407 }
2408
d12307c1
PMR
2409 result.symbol = fixup_symbol_section (result.symbol, objfile);
2410 result.block = block;
2411 return result;
8155455b
DC
2412}
2413
cf901d3b 2414/* See symtab.h. */
5f9a71c3 2415
d12307c1 2416struct block_symbol
f606139a
DE
2417basic_lookup_symbol_nonlocal (const struct language_defn *langdef,
2418 const char *name,
5f9a71c3 2419 const struct block *block,
21b556f4 2420 const domain_enum domain)
5f9a71c3 2421{
d12307c1 2422 struct block_symbol result;
5f9a71c3 2423
d9060ba6
DE
2424 /* NOTE: dje/2014-10-26: The lookup in all objfiles search could skip
2425 the current objfile. Searching the current objfile first is useful
2426 for both matching user expectations as well as performance. */
2427
d12307c1
PMR
2428 result = lookup_symbol_in_static_block (name, block, domain);
2429 if (result.symbol != NULL)
2430 return result;
5f9a71c3 2431
1994afbf
DE
2432 /* If we didn't find a definition for a builtin type in the static block,
2433 search for it now. This is actually the right thing to do and can be
2434 a massive performance win. E.g., when debugging a program with lots of
2435 shared libraries we could search all of them only to find out the
2436 builtin type isn't defined in any of them. This is common for types
2437 like "void". */
2438 if (domain == VAR_DOMAIN)
2439 {
2440 struct gdbarch *gdbarch;
2441
2442 if (block == NULL)
2443 gdbarch = target_gdbarch ();
2444 else
2445 gdbarch = block_gdbarch (block);
d12307c1
PMR
2446 result.symbol = language_lookup_primitive_type_as_symbol (langdef,
2447 gdbarch, name);
2448 result.block = NULL;
2449 if (result.symbol != NULL)
2450 return result;
1994afbf
DE
2451 }
2452
08724ab7 2453 return lookup_global_symbol (name, block, domain);
5f9a71c3
DC
2454}
2455
cf901d3b 2456/* See symtab.h. */
5f9a71c3 2457
d12307c1 2458struct block_symbol
24d864bb
DE
2459lookup_symbol_in_static_block (const char *name,
2460 const struct block *block,
2461 const domain_enum domain)
5f9a71c3
DC
2462{
2463 const struct block *static_block = block_static_block (block);
cc485e62 2464 struct symbol *sym;
5f9a71c3 2465
cc485e62 2466 if (static_block == NULL)
6640a367 2467 return {};
cc485e62
DE
2468
2469 if (symbol_lookup_debug)
2470 {
2471 struct objfile *objfile = lookup_objfile_from_block (static_block);
2472
2473 fprintf_unfiltered (gdb_stdlog,
2474 "lookup_symbol_in_static_block (%s, %s (objfile %s),"
2475 " %s)\n",
2476 name,
2477 host_address_to_string (block),
2478 objfile_debug_name (objfile),
2479 domain_name (domain));
2480 }
2481
de63c46b
PA
2482 sym = lookup_symbol_in_block (name,
2483 symbol_name_match_type::FULL,
2484 static_block, domain);
cc485e62
DE
2485 if (symbol_lookup_debug)
2486 {
2487 fprintf_unfiltered (gdb_stdlog,
2488 "lookup_symbol_in_static_block (...) = %s\n",
2489 sym != NULL ? host_address_to_string (sym) : "NULL");
2490 }
d12307c1 2491 return (struct block_symbol) {sym, static_block};
5f9a71c3
DC
2492}
2493
af3768e9
DE
2494/* Perform the standard symbol lookup of NAME in OBJFILE:
2495 1) First search expanded symtabs, and if not found
2496 2) Search the "quick" symtabs (partial or .gdb_index).
2497 BLOCK_INDEX is one of GLOBAL_BLOCK or STATIC_BLOCK. */
2498
d12307c1 2499static struct block_symbol
c32e6a04 2500lookup_symbol_in_objfile (struct objfile *objfile, enum block_enum block_index,
af3768e9
DE
2501 const char *name, const domain_enum domain)
2502{
d12307c1 2503 struct block_symbol result;
af3768e9 2504
c32e6a04
CB
2505 gdb_assert (block_index == GLOBAL_BLOCK || block_index == STATIC_BLOCK);
2506
cc485e62
DE
2507 if (symbol_lookup_debug)
2508 {
2509 fprintf_unfiltered (gdb_stdlog,
2510 "lookup_symbol_in_objfile (%s, %s, %s, %s)\n",
2511 objfile_debug_name (objfile),
2512 block_index == GLOBAL_BLOCK
2513 ? "GLOBAL_BLOCK" : "STATIC_BLOCK",
2514 name, domain_name (domain));
2515 }
2516
af3768e9
DE
2517 result = lookup_symbol_in_objfile_symtabs (objfile, block_index,
2518 name, domain);
d12307c1 2519 if (result.symbol != NULL)
af3768e9 2520 {
cc485e62
DE
2521 if (symbol_lookup_debug)
2522 {
2523 fprintf_unfiltered (gdb_stdlog,
2524 "lookup_symbol_in_objfile (...) = %s"
2525 " (in symtabs)\n",
d12307c1 2526 host_address_to_string (result.symbol));
cc485e62
DE
2527 }
2528 return result;
af3768e9
DE
2529 }
2530
cc485e62
DE
2531 result = lookup_symbol_via_quick_fns (objfile, block_index,
2532 name, domain);
2533 if (symbol_lookup_debug)
2534 {
2535 fprintf_unfiltered (gdb_stdlog,
2536 "lookup_symbol_in_objfile (...) = %s%s\n",
d12307c1
PMR
2537 result.symbol != NULL
2538 ? host_address_to_string (result.symbol)
cc485e62 2539 : "NULL",
d12307c1 2540 result.symbol != NULL ? " (via quick fns)" : "");
cc485e62 2541 }
af3768e9
DE
2542 return result;
2543}
2544
19630284
JB
2545/* Private data to be used with lookup_symbol_global_iterator_cb. */
2546
9aa55206 2547struct global_or_static_sym_lookup_data
19630284
JB
2548{
2549 /* The name of the symbol we are searching for. */
2550 const char *name;
2551
2552 /* The domain to use for our search. */
2553 domain_enum domain;
2554
9aa55206
CB
2555 /* The block index in which to search. */
2556 enum block_enum block_index;
2557
19630284 2558 /* The field where the callback should store the symbol if found.
d12307c1
PMR
2559 It should be initialized to {NULL, NULL} before the search is started. */
2560 struct block_symbol result;
19630284
JB
2561};
2562
2563/* A callback function for gdbarch_iterate_over_objfiles_in_search_order.
9aa55206
CB
2564 It searches by name for a symbol in the block given by BLOCK_INDEX of the
2565 given OBJFILE. The arguments for the search are passed via CB_DATA, which
2566 in reality is a pointer to struct global_or_static_sym_lookup_data. */
19630284
JB
2567
2568static int
9aa55206
CB
2569lookup_symbol_global_or_static_iterator_cb (struct objfile *objfile,
2570 void *cb_data)
19630284 2571{
9aa55206
CB
2572 struct global_or_static_sym_lookup_data *data =
2573 (struct global_or_static_sym_lookup_data *) cb_data;
19630284 2574
d12307c1
PMR
2575 gdb_assert (data->result.symbol == NULL
2576 && data->result.block == NULL);
19630284 2577
9aa55206 2578 data->result = lookup_symbol_in_objfile (objfile, data->block_index,
af3768e9 2579 data->name, data->domain);
19630284
JB
2580
2581 /* If we found a match, tell the iterator to stop. Otherwise,
2582 keep going. */
d12307c1 2583 return (data->result.symbol != NULL);
19630284
JB
2584}
2585
9aa55206
CB
2586/* This function contains the common code of lookup_{global,static}_symbol.
2587 OBJFILE is only used if BLOCK_INDEX is GLOBAL_SCOPE, in which case it is
2588 the objfile to start the lookup in. */
5f9a71c3 2589
9aa55206
CB
2590static struct block_symbol
2591lookup_global_or_static_symbol (const char *name,
2592 enum block_enum block_index,
2593 struct objfile *objfile,
2594 const domain_enum domain)
5f9a71c3 2595{
f57d2163 2596 struct symbol_cache *cache = get_symbol_cache (current_program_space);
d12307c1 2597 struct block_symbol result;
9aa55206 2598 struct global_or_static_sym_lookup_data lookup_data;
f57d2163
DE
2599 struct block_symbol_cache *bsc;
2600 struct symbol_cache_slot *slot;
b2fb95e0 2601
9aa55206
CB
2602 gdb_assert (block_index == GLOBAL_BLOCK || block_index == STATIC_BLOCK);
2603 gdb_assert (objfile == nullptr || block_index == GLOBAL_BLOCK);
f57d2163
DE
2604
2605 /* First see if we can find the symbol in the cache.
2606 This works because we use the current objfile to qualify the lookup. */
9aa55206 2607 result = symbol_cache_lookup (cache, objfile, block_index, name, domain,
d12307c1
PMR
2608 &bsc, &slot);
2609 if (result.symbol != NULL)
f57d2163 2610 {
d12307c1 2611 if (SYMBOL_LOOKUP_FAILED_P (result))
6640a367 2612 return {};
d12307c1 2613 return result;
f57d2163
DE
2614 }
2615
626ca2c0 2616 /* Do a global search (of global blocks, heh). */
d12307c1 2617 if (result.symbol == NULL)
f57d2163
DE
2618 {
2619 memset (&lookup_data, 0, sizeof (lookup_data));
2620 lookup_data.name = name;
9aa55206 2621 lookup_data.block_index = block_index;
f57d2163
DE
2622 lookup_data.domain = domain;
2623 gdbarch_iterate_over_objfiles_in_search_order
2624 (objfile != NULL ? get_objfile_arch (objfile) : target_gdbarch (),
9aa55206 2625 lookup_symbol_global_or_static_iterator_cb, &lookup_data, objfile);
d12307c1 2626 result = lookup_data.result;
f57d2163 2627 }
6a3ca067 2628
d12307c1
PMR
2629 if (result.symbol != NULL)
2630 symbol_cache_mark_found (bsc, slot, objfile, result.symbol, result.block);
f57d2163
DE
2631 else
2632 symbol_cache_mark_not_found (bsc, slot, objfile, name, domain);
2633
d12307c1 2634 return result;
5f9a71c3
DC
2635}
2636
9aa55206
CB
2637/* See symtab.h. */
2638
2639struct block_symbol
2640lookup_static_symbol (const char *name, const domain_enum domain)
2641{
2642 return lookup_global_or_static_symbol (name, STATIC_BLOCK, nullptr, domain);
2643}
2644
2645/* See symtab.h. */
2646
2647struct block_symbol
2648lookup_global_symbol (const char *name,
2649 const struct block *block,
2650 const domain_enum domain)
2651{
d3d32391
AB
2652 /* If a block was passed in, we want to search the corresponding
2653 global block first. This yields "more expected" behavior, and is
2654 needed to support 'FILENAME'::VARIABLE lookups. */
2655 const struct block *global_block = block_global_block (block);
2656 if (global_block != nullptr)
2657 {
2658 symbol *sym = lookup_symbol_in_block (name,
2659 symbol_name_match_type::FULL,
2660 global_block, domain);
2661 if (sym != nullptr)
2662 return { sym, global_block };
2663 }
2664
9aa55206
CB
2665 struct objfile *objfile = lookup_objfile_from_block (block);
2666 return lookup_global_or_static_symbol (name, GLOBAL_BLOCK, objfile, domain);
2667}
2668
ececd218 2669bool
4186eb54
KS
2670symbol_matches_domain (enum language symbol_language,
2671 domain_enum symbol_domain,
2672 domain_enum domain)
2673{
2674 /* For C++ "struct foo { ... }" also defines a typedef for "foo".
4186eb54
KS
2675 Similarly, any Ada type declaration implicitly defines a typedef. */
2676 if (symbol_language == language_cplus
2677 || symbol_language == language_d
65547233
TT
2678 || symbol_language == language_ada
2679 || symbol_language == language_rust)
4186eb54
KS
2680 {
2681 if ((domain == VAR_DOMAIN || domain == STRUCT_DOMAIN)
2682 && symbol_domain == STRUCT_DOMAIN)
ececd218 2683 return true;
4186eb54
KS
2684 }
2685 /* For all other languages, strict match is required. */
2686 return (symbol_domain == domain);
2687}
2688
cf901d3b 2689/* See symtab.h. */
c906108c 2690
ccefe4c4
TT
2691struct type *
2692lookup_transparent_type (const char *name)
c906108c 2693{
ccefe4c4
TT
2694 return current_language->la_lookup_transparent_type (name);
2695}
9af17804 2696
ccefe4c4
TT
2697/* A helper for basic_lookup_transparent_type that interfaces with the
2698 "quick" symbol table functions. */
357e46e7 2699
ccefe4c4 2700static struct type *
ddbcedf5
CB
2701basic_lookup_transparent_type_quick (struct objfile *objfile,
2702 enum block_enum block_index,
ccefe4c4
TT
2703 const char *name)
2704{
43f3e411 2705 struct compunit_symtab *cust;
346d1dfe 2706 const struct blockvector *bv;
582942f4 2707 const struct block *block;
ccefe4c4 2708 struct symbol *sym;
c906108c 2709
ccefe4c4
TT
2710 if (!objfile->sf)
2711 return NULL;
43f3e411
DE
2712 cust = objfile->sf->qf->lookup_symbol (objfile, block_index, name,
2713 STRUCT_DOMAIN);
2714 if (cust == NULL)
ccefe4c4 2715 return NULL;
c906108c 2716
43f3e411 2717 bv = COMPUNIT_BLOCKVECTOR (cust);
f88cb4b6 2718 block = BLOCKVECTOR_BLOCK (bv, block_index);
b2e2f908
DE
2719 sym = block_find_symbol (block, name, STRUCT_DOMAIN,
2720 block_find_non_opaque_type, NULL);
2721 if (sym == NULL)
43f3e411 2722 error_in_psymtab_expansion (block_index, name, cust);
b2e2f908
DE
2723 gdb_assert (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)));
2724 return SYMBOL_TYPE (sym);
2725}
08c23b0d 2726
b2e2f908
DE
2727/* Subroutine of basic_lookup_transparent_type to simplify it.
2728 Look up the non-opaque definition of NAME in BLOCK_INDEX of OBJFILE.
2729 BLOCK_INDEX is either GLOBAL_BLOCK or STATIC_BLOCK. */
2730
2731static struct type *
ddbcedf5
CB
2732basic_lookup_transparent_type_1 (struct objfile *objfile,
2733 enum block_enum block_index,
b2e2f908
DE
2734 const char *name)
2735{
b2e2f908
DE
2736 const struct blockvector *bv;
2737 const struct block *block;
2738 const struct symbol *sym;
2739
b669c953 2740 for (compunit_symtab *cust : objfile->compunits ())
b2e2f908
DE
2741 {
2742 bv = COMPUNIT_BLOCKVECTOR (cust);
2743 block = BLOCKVECTOR_BLOCK (bv, block_index);
2744 sym = block_find_symbol (block, name, STRUCT_DOMAIN,
2745 block_find_non_opaque_type, NULL);
2746 if (sym != NULL)
2747 {
2748 gdb_assert (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)));
2749 return SYMBOL_TYPE (sym);
2750 }
2751 }
c906108c 2752
ccefe4c4 2753 return NULL;
b368761e 2754}
c906108c 2755
b368761e
DC
2756/* The standard implementation of lookup_transparent_type. This code
2757 was modeled on lookup_symbol -- the parts not relevant to looking
2758 up types were just left out. In particular it's assumed here that
cf901d3b 2759 types are available in STRUCT_DOMAIN and only in file-static or
b368761e 2760 global blocks. */
c906108c
SS
2761
2762struct type *
b368761e 2763basic_lookup_transparent_type (const char *name)
c906108c 2764{
ccefe4c4 2765 struct type *t;
c906108c
SS
2766
2767 /* Now search all the global symbols. Do the symtab's first, then
c378eb4e 2768 check the psymtab's. If a psymtab indicates the existence
c906108c
SS
2769 of the desired name as a global, then do psymtab-to-symtab
2770 conversion on the fly and return the found symbol. */
c5aa993b 2771
2030c079 2772 for (objfile *objfile : current_program_space->objfiles ())
aed57c53
TT
2773 {
2774 t = basic_lookup_transparent_type_1 (objfile, GLOBAL_BLOCK, name);
2775 if (t)
2776 return t;
2777 }
c906108c 2778
2030c079 2779 for (objfile *objfile : current_program_space->objfiles ())
aed57c53
TT
2780 {
2781 t = basic_lookup_transparent_type_quick (objfile, GLOBAL_BLOCK, name);
2782 if (t)
2783 return t;
2784 }
c906108c
SS
2785
2786 /* Now search the static file-level symbols.
2787 Not strictly correct, but more useful than an error.
2788 Do the symtab's first, then
c378eb4e 2789 check the psymtab's. If a psymtab indicates the existence
c906108c 2790 of the desired name as a file-level static, then do psymtab-to-symtab
c378eb4e 2791 conversion on the fly and return the found symbol. */
c906108c 2792
2030c079 2793 for (objfile *objfile : current_program_space->objfiles ())
aed57c53
TT
2794 {
2795 t = basic_lookup_transparent_type_1 (objfile, STATIC_BLOCK, name);
2796 if (t)
2797 return t;
2798 }
c906108c 2799
2030c079 2800 for (objfile *objfile : current_program_space->objfiles ())
aed57c53
TT
2801 {
2802 t = basic_lookup_transparent_type_quick (objfile, STATIC_BLOCK, name);
2803 if (t)
2804 return t;
2805 }
ccefe4c4 2806
c906108c
SS
2807 return (struct type *) 0;
2808}
2809
6969f124 2810/* See symtab.h. */
f8eba3c6 2811
6969f124 2812bool
b5ec771e
PA
2813iterate_over_symbols (const struct block *block,
2814 const lookup_name_info &name,
f8eba3c6 2815 const domain_enum domain,
14bc53a8 2816 gdb::function_view<symbol_found_callback_ftype> callback)
f8eba3c6 2817{
4eeaa230
DE
2818 struct block_iterator iter;
2819 struct symbol *sym;
f8eba3c6 2820
358d6ab3 2821 ALL_BLOCK_SYMBOLS_WITH_NAME (block, name, iter, sym)
4eeaa230 2822 {
4186eb54
KS
2823 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
2824 SYMBOL_DOMAIN (sym), domain))
f8eba3c6 2825 {
7e41c8db
KS
2826 struct block_symbol block_sym = {sym, block};
2827
2828 if (!callback (&block_sym))
6969f124 2829 return false;
f8eba3c6 2830 }
f8eba3c6 2831 }
6969f124 2832 return true;
f8eba3c6
TT
2833}
2834
6a3dbf1b
TT
2835/* See symtab.h. */
2836
2837bool
2838iterate_over_symbols_terminated
2839 (const struct block *block,
2840 const lookup_name_info &name,
2841 const domain_enum domain,
2842 gdb::function_view<symbol_found_callback_ftype> callback)
2843{
2844 if (!iterate_over_symbols (block, name, domain, callback))
2845 return false;
2846 struct block_symbol block_sym = {nullptr, block};
2847 return callback (&block_sym);
2848}
2849
43f3e411
DE
2850/* Find the compunit symtab associated with PC and SECTION.
2851 This will read in debug info as necessary. */
c906108c 2852
43f3e411
DE
2853struct compunit_symtab *
2854find_pc_sect_compunit_symtab (CORE_ADDR pc, struct obj_section *section)
c906108c 2855{
43f3e411 2856 struct compunit_symtab *best_cust = NULL;
c906108c 2857 CORE_ADDR distance = 0;
77e371c0 2858 struct bound_minimal_symbol msymbol;
8a48e967
DJ
2859
2860 /* If we know that this is not a text address, return failure. This is
2861 necessary because we loop based on the block's high and low code
2862 addresses, which do not include the data ranges, and because
2863 we call find_pc_sect_psymtab which has a similar restriction based
2864 on the partial_symtab's texthigh and textlow. */
77e371c0 2865 msymbol = lookup_minimal_symbol_by_pc_section (pc, section);
1ed9f74e 2866 if (msymbol.minsym && msymbol.minsym->data_p ())
8a48e967 2867 return NULL;
c906108c
SS
2868
2869 /* Search all symtabs for the one whose file contains our address, and which
2870 is the smallest of all the ones containing the address. This is designed
2871 to deal with a case like symtab a is at 0x1000-0x2000 and 0x3000-0x4000
2872 and symtab b is at 0x2000-0x3000. So the GLOBAL_BLOCK for a is from
2873 0x1000-0x4000, but for address 0x2345 we want to return symtab b.
2874
2875 This happens for native ecoff format, where code from included files
c378eb4e 2876 gets its own symtab. The symtab for the included file should have
c906108c
SS
2877 been read in already via the dependency mechanism.
2878 It might be swifter to create several symtabs with the same name
2879 like xcoff does (I'm not sure).
2880
2881 It also happens for objfiles that have their functions reordered.
2882 For these, the symtab we are looking for is not necessarily read in. */
2883
2030c079 2884 for (objfile *obj_file : current_program_space->objfiles ())
d8aeb77f 2885 {
b669c953 2886 for (compunit_symtab *cust : obj_file->compunits ())
d8aeb77f 2887 {
582942f4 2888 const struct block *b;
d8aeb77f 2889 const struct blockvector *bv;
43f3e411 2890
d8aeb77f
TT
2891 bv = COMPUNIT_BLOCKVECTOR (cust);
2892 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
c906108c 2893
d8aeb77f
TT
2894 if (BLOCK_START (b) <= pc
2895 && BLOCK_END (b) > pc
2896 && (distance == 0
2897 || BLOCK_END (b) - BLOCK_START (b) < distance))
2898 {
2899 /* For an objfile that has its functions reordered,
2900 find_pc_psymtab will find the proper partial symbol table
2901 and we simply return its corresponding symtab. */
2902 /* In order to better support objfiles that contain both
2903 stabs and coff debugging info, we continue on if a psymtab
2904 can't be found. */
2905 if ((obj_file->flags & OBJF_REORDERED) && obj_file->sf)
2906 {
2907 struct compunit_symtab *result;
2908
2909 result
2910 = obj_file->sf->qf->find_pc_sect_compunit_symtab (obj_file,
2911 msymbol,
2912 pc,
2913 section,
2914 0);
2915 if (result != NULL)
2916 return result;
2917 }
2918 if (section != 0)
2919 {
2920 struct block_iterator iter;
2921 struct symbol *sym = NULL;
c906108c 2922
d8aeb77f
TT
2923 ALL_BLOCK_SYMBOLS (b, iter, sym)
2924 {
2925 fixup_symbol_section (sym, obj_file);
2926 if (matching_obj_sections (SYMBOL_OBJ_SECTION (obj_file,
2927 sym),
2928 section))
2929 break;
2930 }
2931 if (sym == NULL)
2932 continue; /* No symbol in this symtab matches
2933 section. */
2934 }
2935 distance = BLOCK_END (b) - BLOCK_START (b);
2936 best_cust = cust;
2937 }
2938 }
2939 }
c906108c 2940
43f3e411
DE
2941 if (best_cust != NULL)
2942 return best_cust;
c906108c 2943
072cabfe
DE
2944 /* Not found in symtabs, search the "quick" symtabs (e.g. psymtabs). */
2945
2030c079 2946 for (objfile *objf : current_program_space->objfiles ())
aed57c53
TT
2947 {
2948 struct compunit_symtab *result;
2949
2950 if (!objf->sf)
2951 continue;
2952 result = objf->sf->qf->find_pc_sect_compunit_symtab (objf,
2953 msymbol,
2954 pc, section,
2955 1);
2956 if (result != NULL)
2957 return result;
2958 }
ccefe4c4
TT
2959
2960 return NULL;
c906108c
SS
2961}
2962
43f3e411
DE
2963/* Find the compunit symtab associated with PC.
2964 This will read in debug info as necessary.
2965 Backward compatibility, no section. */
c906108c 2966
43f3e411
DE
2967struct compunit_symtab *
2968find_pc_compunit_symtab (CORE_ADDR pc)
c906108c 2969{
43f3e411 2970 return find_pc_sect_compunit_symtab (pc, find_pc_mapped_section (pc));
c906108c 2971}
71a3c369
TT
2972
2973/* See symtab.h. */
2974
2975struct symbol *
2976find_symbol_at_address (CORE_ADDR address)
2977{
2030c079 2978 for (objfile *objfile : current_program_space->objfiles ())
aed57c53
TT
2979 {
2980 if (objfile->sf == NULL
2981 || objfile->sf->qf->find_compunit_symtab_by_address == NULL)
2982 continue;
71a3c369 2983
aed57c53
TT
2984 struct compunit_symtab *symtab
2985 = objfile->sf->qf->find_compunit_symtab_by_address (objfile, address);
2986 if (symtab != NULL)
2987 {
2988 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (symtab);
71a3c369 2989
aed57c53 2990 for (int i = GLOBAL_BLOCK; i <= STATIC_BLOCK; ++i)
71a3c369 2991 {
582942f4 2992 const struct block *b = BLOCKVECTOR_BLOCK (bv, i);
aed57c53
TT
2993 struct block_iterator iter;
2994 struct symbol *sym;
2995
2996 ALL_BLOCK_SYMBOLS (b, iter, sym)
2997 {
2998 if (SYMBOL_CLASS (sym) == LOC_STATIC
2999 && SYMBOL_VALUE_ADDRESS (sym) == address)
3000 return sym;
3001 }
71a3c369 3002 }
aed57c53
TT
3003 }
3004 }
71a3c369
TT
3005
3006 return NULL;
3007}
3008
c906108c 3009\f
c5aa993b 3010
7e73cedf 3011/* Find the source file and line number for a given PC value and SECTION.
c906108c
SS
3012 Return a structure containing a symtab pointer, a line number,
3013 and a pc range for the entire source line.
3014 The value's .pc field is NOT the specified pc.
3015 NOTCURRENT nonzero means, if specified pc is on a line boundary,
3016 use the line that ends there. Otherwise, in that case, the line
3017 that begins there is used. */
3018
3019/* The big complication here is that a line may start in one file, and end just
3020 before the start of another file. This usually occurs when you #include
3021 code in the middle of a subroutine. To properly find the end of a line's PC
3022 range, we must search all symtabs associated with this compilation unit, and
3023 find the one whose first PC is closer than that of the next line in this
3024 symtab. */
3025
c906108c 3026struct symtab_and_line
714835d5 3027find_pc_sect_line (CORE_ADDR pc, struct obj_section *section, int notcurrent)
c906108c 3028{
43f3e411 3029 struct compunit_symtab *cust;
52f0bd74
AC
3030 struct linetable *l;
3031 int len;
52f0bd74 3032 struct linetable_entry *item;
346d1dfe 3033 const struct blockvector *bv;
7cbd4a93 3034 struct bound_minimal_symbol msymbol;
c906108c
SS
3035
3036 /* Info on best line seen so far, and where it starts, and its file. */
3037
3038 struct linetable_entry *best = NULL;
3039 CORE_ADDR best_end = 0;
3040 struct symtab *best_symtab = 0;
3041
3042 /* Store here the first line number
3043 of a file which contains the line at the smallest pc after PC.
3044 If we don't find a line whose range contains PC,
3045 we will use a line one less than this,
3046 with a range from the start of that file to the first line's pc. */
3047 struct linetable_entry *alt = NULL;
c906108c
SS
3048
3049 /* Info on best line seen in this file. */
3050
3051 struct linetable_entry *prev;
3052
3053 /* If this pc is not from the current frame,
3054 it is the address of the end of a call instruction.
3055 Quite likely that is the start of the following statement.
3056 But what we want is the statement containing the instruction.
3057 Fudge the pc to make sure we get that. */
3058
b77b1eb7
JB
3059 /* It's tempting to assume that, if we can't find debugging info for
3060 any function enclosing PC, that we shouldn't search for line
3061 number info, either. However, GAS can emit line number info for
3062 assembly files --- very helpful when debugging hand-written
3063 assembly code. In such a case, we'd have no debug info for the
3064 function, but we would have line info. */
648f4f79 3065
c906108c
SS
3066 if (notcurrent)
3067 pc -= 1;
3068
c5aa993b 3069 /* elz: added this because this function returned the wrong
c906108c 3070 information if the pc belongs to a stub (import/export)
c378eb4e 3071 to call a shlib function. This stub would be anywhere between
9af17804 3072 two functions in the target, and the line info was erroneously
c378eb4e
MS
3073 taken to be the one of the line before the pc. */
3074
c906108c 3075 /* RT: Further explanation:
c5aa993b 3076
c906108c
SS
3077 * We have stubs (trampolines) inserted between procedures.
3078 *
3079 * Example: "shr1" exists in a shared library, and a "shr1" stub also
3080 * exists in the main image.
3081 *
3082 * In the minimal symbol table, we have a bunch of symbols
c378eb4e 3083 * sorted by start address. The stubs are marked as "trampoline",
c906108c
SS
3084 * the others appear as text. E.g.:
3085 *
9af17804 3086 * Minimal symbol table for main image
c906108c
SS
3087 * main: code for main (text symbol)
3088 * shr1: stub (trampoline symbol)
3089 * foo: code for foo (text symbol)
3090 * ...
3091 * Minimal symbol table for "shr1" image:
3092 * ...
3093 * shr1: code for shr1 (text symbol)
3094 * ...
3095 *
3096 * So the code below is trying to detect if we are in the stub
3097 * ("shr1" stub), and if so, find the real code ("shr1" trampoline),
3098 * and if found, do the symbolization from the real-code address
3099 * rather than the stub address.
3100 *
3101 * Assumptions being made about the minimal symbol table:
3102 * 1. lookup_minimal_symbol_by_pc() will return a trampoline only
c378eb4e 3103 * if we're really in the trampoline.s If we're beyond it (say
9af17804 3104 * we're in "foo" in the above example), it'll have a closer
c906108c
SS
3105 * symbol (the "foo" text symbol for example) and will not
3106 * return the trampoline.
3107 * 2. lookup_minimal_symbol_text() will find a real text symbol
3108 * corresponding to the trampoline, and whose address will
c378eb4e 3109 * be different than the trampoline address. I put in a sanity
c906108c
SS
3110 * check for the address being the same, to avoid an
3111 * infinite recursion.
3112 */
c5aa993b 3113 msymbol = lookup_minimal_symbol_by_pc (pc);
7cbd4a93
TT
3114 if (msymbol.minsym != NULL)
3115 if (MSYMBOL_TYPE (msymbol.minsym) == mst_solib_trampoline)
c5aa993b 3116 {
77e371c0 3117 struct bound_minimal_symbol mfunsym
efd66ac6 3118 = lookup_minimal_symbol_text (MSYMBOL_LINKAGE_NAME (msymbol.minsym),
77e371c0
TT
3119 NULL);
3120
3121 if (mfunsym.minsym == NULL)
c5aa993b
JM
3122 /* I eliminated this warning since it is coming out
3123 * in the following situation:
3124 * gdb shmain // test program with shared libraries
3125 * (gdb) break shr1 // function in shared lib
3126 * Warning: In stub for ...
9af17804 3127 * In the above situation, the shared lib is not loaded yet,
c5aa993b
JM
3128 * so of course we can't find the real func/line info,
3129 * but the "break" still works, and the warning is annoying.
c378eb4e 3130 * So I commented out the warning. RT */
3e43a32a 3131 /* warning ("In stub for %s; unable to find real function/line info",
c378eb4e
MS
3132 SYMBOL_LINKAGE_NAME (msymbol)); */
3133 ;
c5aa993b 3134 /* fall through */
77e371c0
TT
3135 else if (BMSYMBOL_VALUE_ADDRESS (mfunsym)
3136 == BMSYMBOL_VALUE_ADDRESS (msymbol))
c5aa993b 3137 /* Avoid infinite recursion */
c378eb4e 3138 /* See above comment about why warning is commented out. */
3e43a32a 3139 /* warning ("In stub for %s; unable to find real function/line info",
c378eb4e
MS
3140 SYMBOL_LINKAGE_NAME (msymbol)); */
3141 ;
c5aa993b
JM
3142 /* fall through */
3143 else
77e371c0 3144 return find_pc_line (BMSYMBOL_VALUE_ADDRESS (mfunsym), 0);
c5aa993b 3145 }
c906108c 3146
51abb421
PA
3147 symtab_and_line val;
3148 val.pspace = current_program_space;
c906108c 3149
43f3e411
DE
3150 cust = find_pc_sect_compunit_symtab (pc, section);
3151 if (cust == NULL)
c906108c 3152 {
c378eb4e 3153 /* If no symbol information, return previous pc. */
c906108c
SS
3154 if (notcurrent)
3155 pc++;
3156 val.pc = pc;
3157 return val;
3158 }
3159
43f3e411 3160 bv = COMPUNIT_BLOCKVECTOR (cust);
c906108c
SS
3161
3162 /* Look at all the symtabs that share this blockvector.
3163 They all have the same apriori range, that we found was right;
3164 but they have different line tables. */
3165
5accd1a0 3166 for (symtab *iter_s : compunit_filetabs (cust))
c906108c
SS
3167 {
3168 /* Find the best line in this symtab. */
43f3e411 3169 l = SYMTAB_LINETABLE (iter_s);
c906108c 3170 if (!l)
c5aa993b 3171 continue;
c906108c
SS
3172 len = l->nitems;
3173 if (len <= 0)
3174 {
3175 /* I think len can be zero if the symtab lacks line numbers
3176 (e.g. gcc -g1). (Either that or the LINETABLE is NULL;
3177 I'm not sure which, and maybe it depends on the symbol
3178 reader). */
3179 continue;
3180 }
3181
3182 prev = NULL;
c378eb4e 3183 item = l->item; /* Get first line info. */
c906108c
SS
3184
3185 /* Is this file's first line closer than the first lines of other files?
c5aa993b 3186 If so, record this file, and its first line, as best alternate. */
c906108c 3187 if (item->pc > pc && (!alt || item->pc < alt->pc))
c656bca5 3188 alt = item;
c906108c 3189
b926417a 3190 auto pc_compare = [](const CORE_ADDR & comp_pc,
7cbe16e9
SR
3191 const struct linetable_entry & lhs)->bool
3192 {
b926417a 3193 return comp_pc < lhs.pc;
7cbe16e9 3194 };
c906108c 3195
7cbe16e9
SR
3196 struct linetable_entry *first = item;
3197 struct linetable_entry *last = item + len;
3198 item = std::upper_bound (first, last, pc, pc_compare);
3199 if (item != first)
3200 prev = item - 1; /* Found a matching item. */
c906108c
SS
3201
3202 /* At this point, prev points at the line whose start addr is <= pc, and
c5aa993b
JM
3203 item points at the next line. If we ran off the end of the linetable
3204 (pc >= start of the last line), then prev == item. If pc < start of
3205 the first line, prev will not be set. */
c906108c
SS
3206
3207 /* Is this file's best line closer than the best in the other files?
083ae935
DJ
3208 If so, record this file, and its best line, as best so far. Don't
3209 save prev if it represents the end of a function (i.e. line number
3210 0) instead of a real line. */
c906108c 3211
083ae935 3212 if (prev && prev->line && (!best || prev->pc > best->pc))
c906108c
SS
3213 {
3214 best = prev;
43f3e411 3215 best_symtab = iter_s;
25d53da1
KB
3216
3217 /* Discard BEST_END if it's before the PC of the current BEST. */
3218 if (best_end <= best->pc)
3219 best_end = 0;
c906108c 3220 }
25d53da1
KB
3221
3222 /* If another line (denoted by ITEM) is in the linetable and its
7cbe16e9 3223 PC is after BEST's PC, but before the current BEST_END, then
25d53da1 3224 use ITEM's PC as the new best_end. */
4ee89e90 3225 if (best && item < last && item->pc > best->pc
7cbe16e9 3226 && (best_end == 0 || best_end > item->pc))
25d53da1 3227 best_end = item->pc;
c906108c
SS
3228 }
3229
3230 if (!best_symtab)
3231 {
e86e87f7
DJ
3232 /* If we didn't find any line number info, just return zeros.
3233 We used to return alt->line - 1 here, but that could be
3234 anywhere; if we don't have line number info for this PC,
3235 don't make some up. */
3236 val.pc = pc;
c906108c 3237 }
e8717518
FF
3238 else if (best->line == 0)
3239 {
3240 /* If our best fit is in a range of PC's for which no line
3241 number info is available (line number is zero) then we didn't
c378eb4e 3242 find any valid line information. */
e8717518
FF
3243 val.pc = pc;
3244 }
c906108c
SS
3245 else
3246 {
3247 val.symtab = best_symtab;
3248 val.line = best->line;
3249 val.pc = best->pc;
3250 if (best_end && (!alt || best_end < alt->pc))
3251 val.end = best_end;
3252 else if (alt)
3253 val.end = alt->pc;
3254 else
3255 val.end = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
3256 }
3257 val.section = section;
3258 return val;
3259}
3260
c378eb4e 3261/* Backward compatibility (no section). */
c906108c
SS
3262
3263struct symtab_and_line
fba45db2 3264find_pc_line (CORE_ADDR pc, int notcurrent)
c906108c 3265{
714835d5 3266 struct obj_section *section;
c906108c
SS
3267
3268 section = find_pc_overlay (pc);
3269 if (pc_in_unmapped_range (pc, section))
3270 pc = overlay_mapped_address (pc, section);
3271 return find_pc_sect_line (pc, section, notcurrent);
3272}
34248c3a
DE
3273
3274/* See symtab.h. */
3275
3276struct symtab *
3277find_pc_line_symtab (CORE_ADDR pc)
3278{
3279 struct symtab_and_line sal;
3280
3281 /* This always passes zero for NOTCURRENT to find_pc_line.
3282 There are currently no callers that ever pass non-zero. */
3283 sal = find_pc_line (pc, 0);
3284 return sal.symtab;
3285}
c906108c 3286\f
c906108c
SS
3287/* Find line number LINE in any symtab whose name is the same as
3288 SYMTAB.
3289
3290 If found, return the symtab that contains the linetable in which it was
3291 found, set *INDEX to the index in the linetable of the best entry
ececd218 3292 found, and set *EXACT_MATCH to true if the value returned is an
c906108c
SS
3293 exact match.
3294
3295 If not found, return NULL. */
3296
50641945 3297struct symtab *
5accd1a0 3298find_line_symtab (struct symtab *sym_tab, int line,
ececd218 3299 int *index, bool *exact_match)
c906108c 3300{
6f43c46f 3301 int exact = 0; /* Initialized here to avoid a compiler warning. */
c906108c
SS
3302
3303 /* BEST_INDEX and BEST_LINETABLE identify the smallest linenumber > LINE
3304 so far seen. */
3305
3306 int best_index;
3307 struct linetable *best_linetable;
3308 struct symtab *best_symtab;
3309
3310 /* First try looking it up in the given symtab. */
5accd1a0
TT
3311 best_linetable = SYMTAB_LINETABLE (sym_tab);
3312 best_symtab = sym_tab;
f8eba3c6 3313 best_index = find_line_common (best_linetable, line, &exact, 0);
c906108c
SS
3314 if (best_index < 0 || !exact)
3315 {
3316 /* Didn't find an exact match. So we better keep looking for
c5aa993b
JM
3317 another symtab with the same name. In the case of xcoff,
3318 multiple csects for one source file (produced by IBM's FORTRAN
3319 compiler) produce multiple symtabs (this is unavoidable
3320 assuming csects can be at arbitrary places in memory and that
3321 the GLOBAL_BLOCK of a symtab has a begin and end address). */
c906108c
SS
3322
3323 /* BEST is the smallest linenumber > LINE so far seen,
c5aa993b
JM
3324 or 0 if none has been seen so far.
3325 BEST_INDEX and BEST_LINETABLE identify the item for it. */
c906108c
SS
3326 int best;
3327
c906108c
SS
3328 if (best_index >= 0)
3329 best = best_linetable->item[best_index].line;
3330 else
3331 best = 0;
3332
2030c079 3333 for (objfile *objfile : current_program_space->objfiles ())
aed57c53
TT
3334 {
3335 if (objfile->sf)
3336 objfile->sf->qf->expand_symtabs_with_fullname
5accd1a0 3337 (objfile, symtab_to_fullname (sym_tab));
aed57c53 3338 }
51432cca 3339
2030c079 3340 for (objfile *objfile : current_program_space->objfiles ())
8b31193a 3341 {
b669c953 3342 for (compunit_symtab *cu : objfile->compunits ())
8b31193a
TT
3343 {
3344 for (symtab *s : compunit_filetabs (cu))
3345 {
3346 struct linetable *l;
3347 int ind;
3348
3349 if (FILENAME_CMP (sym_tab->filename, s->filename) != 0)
3350 continue;
3351 if (FILENAME_CMP (symtab_to_fullname (sym_tab),
3352 symtab_to_fullname (s)) != 0)
3353 continue;
3354 l = SYMTAB_LINETABLE (s);
3355 ind = find_line_common (l, line, &exact, 0);
3356 if (ind >= 0)
3357 {
3358 if (exact)
3359 {
3360 best_index = ind;
3361 best_linetable = l;
3362 best_symtab = s;
3363 goto done;
3364 }
3365 if (best == 0 || l->item[ind].line < best)
3366 {
3367 best = l->item[ind].line;
3368 best_index = ind;
3369 best_linetable = l;
3370 best_symtab = s;
3371 }
3372 }
3373 }
3374 }
3375 }
c906108c 3376 }
c5aa993b 3377done:
c906108c
SS
3378 if (best_index < 0)
3379 return NULL;
3380
3381 if (index)
3382 *index = best_index;
3383 if (exact_match)
ececd218 3384 *exact_match = (exact != 0);
c906108c
SS
3385
3386 return best_symtab;
3387}
f8eba3c6
TT
3388
3389/* Given SYMTAB, returns all the PCs function in the symtab that
67d89901
TT
3390 exactly match LINE. Returns an empty vector if there are no exact
3391 matches, but updates BEST_ITEM in this case. */
f8eba3c6 3392
67d89901 3393std::vector<CORE_ADDR>
f8eba3c6
TT
3394find_pcs_for_symtab_line (struct symtab *symtab, int line,
3395 struct linetable_entry **best_item)
3396{
c656bca5 3397 int start = 0;
67d89901 3398 std::vector<CORE_ADDR> result;
f8eba3c6
TT
3399
3400 /* First, collect all the PCs that are at this line. */
3401 while (1)
3402 {
3403 int was_exact;
3404 int idx;
3405
8435453b
DE
3406 idx = find_line_common (SYMTAB_LINETABLE (symtab), line, &was_exact,
3407 start);
f8eba3c6
TT
3408 if (idx < 0)
3409 break;
3410
3411 if (!was_exact)
3412 {
8435453b 3413 struct linetable_entry *item = &SYMTAB_LINETABLE (symtab)->item[idx];
f8eba3c6
TT
3414
3415 if (*best_item == NULL || item->line < (*best_item)->line)
3416 *best_item = item;
3417
3418 break;
3419 }
3420
67d89901 3421 result.push_back (SYMTAB_LINETABLE (symtab)->item[idx].pc);
f8eba3c6
TT
3422 start = idx + 1;
3423 }
3424
3425 return result;
3426}
3427
c906108c
SS
3428\f
3429/* Set the PC value for a given source file and line number and return true.
ececd218 3430 Returns false for invalid line number (and sets the PC to 0).
c906108c
SS
3431 The source file is specified with a struct symtab. */
3432
ececd218 3433bool
fba45db2 3434find_line_pc (struct symtab *symtab, int line, CORE_ADDR *pc)
c906108c
SS
3435{
3436 struct linetable *l;
3437 int ind;
3438
3439 *pc = 0;
3440 if (symtab == 0)
ececd218 3441 return false;
c906108c
SS
3442
3443 symtab = find_line_symtab (symtab, line, &ind, NULL);
3444 if (symtab != NULL)
3445 {
8435453b 3446 l = SYMTAB_LINETABLE (symtab);
c906108c 3447 *pc = l->item[ind].pc;
ececd218 3448 return true;
c906108c
SS
3449 }
3450 else
ececd218 3451 return false;
c906108c
SS
3452}
3453
3454/* Find the range of pc values in a line.
3455 Store the starting pc of the line into *STARTPTR
3456 and the ending pc (start of next line) into *ENDPTR.
ececd218
CB
3457 Returns true to indicate success.
3458 Returns false if could not find the specified line. */
c906108c 3459
ececd218 3460bool
fba45db2
KB
3461find_line_pc_range (struct symtab_and_line sal, CORE_ADDR *startptr,
3462 CORE_ADDR *endptr)
c906108c
SS
3463{
3464 CORE_ADDR startaddr;
3465 struct symtab_and_line found_sal;
3466
3467 startaddr = sal.pc;
c5aa993b 3468 if (startaddr == 0 && !find_line_pc (sal.symtab, sal.line, &startaddr))
ececd218 3469 return false;
c906108c
SS
3470
3471 /* This whole function is based on address. For example, if line 10 has
3472 two parts, one from 0x100 to 0x200 and one from 0x300 to 0x400, then
3473 "info line *0x123" should say the line goes from 0x100 to 0x200
3474 and "info line *0x355" should say the line goes from 0x300 to 0x400.
3475 This also insures that we never give a range like "starts at 0x134
3476 and ends at 0x12c". */
3477
3478 found_sal = find_pc_sect_line (startaddr, sal.section, 0);
3479 if (found_sal.line != sal.line)
3480 {
3481 /* The specified line (sal) has zero bytes. */
3482 *startptr = found_sal.pc;
3483 *endptr = found_sal.pc;
3484 }
3485 else
3486 {
3487 *startptr = found_sal.pc;
3488 *endptr = found_sal.end;
3489 }
ececd218 3490 return true;
c906108c
SS
3491}
3492
3493/* Given a line table and a line number, return the index into the line
3494 table for the pc of the nearest line whose number is >= the specified one.
3495 Return -1 if none is found. The value is >= 0 if it is an index.
f8eba3c6 3496 START is the index at which to start searching the line table.
c906108c
SS
3497
3498 Set *EXACT_MATCH nonzero if the value returned is an exact match. */
3499
3500static int
aa1ee363 3501find_line_common (struct linetable *l, int lineno,
f8eba3c6 3502 int *exact_match, int start)
c906108c 3503{
52f0bd74
AC
3504 int i;
3505 int len;
c906108c
SS
3506
3507 /* BEST is the smallest linenumber > LINENO so far seen,
3508 or 0 if none has been seen so far.
3509 BEST_INDEX identifies the item for it. */
3510
3511 int best_index = -1;
3512 int best = 0;
3513
b7589f7d
DJ
3514 *exact_match = 0;
3515
c906108c
SS
3516 if (lineno <= 0)
3517 return -1;
3518 if (l == 0)
3519 return -1;
3520
3521 len = l->nitems;
f8eba3c6 3522 for (i = start; i < len; i++)
c906108c 3523 {
aa1ee363 3524 struct linetable_entry *item = &(l->item[i]);
c906108c
SS
3525
3526 if (item->line == lineno)
3527 {
3528 /* Return the first (lowest address) entry which matches. */
3529 *exact_match = 1;
3530 return i;
3531 }
3532
3533 if (item->line > lineno && (best == 0 || item->line < best))
3534 {
3535 best = item->line;
3536 best_index = i;
3537 }
3538 }
3539
3540 /* If we got here, we didn't get an exact match. */
c906108c
SS
3541 return best_index;
3542}
3543
ececd218 3544bool
fba45db2 3545find_pc_line_pc_range (CORE_ADDR pc, CORE_ADDR *startptr, CORE_ADDR *endptr)
c906108c
SS
3546{
3547 struct symtab_and_line sal;
433759f7 3548
c906108c
SS
3549 sal = find_pc_line (pc, 0);
3550 *startptr = sal.pc;
3551 *endptr = sal.end;
3552 return sal.symtab != 0;
3553}
3554
cd2bb709
PA
3555/* Helper for find_function_start_sal. Does most of the work, except
3556 setting the sal's symbol. */
aab2f208 3557
cd2bb709
PA
3558static symtab_and_line
3559find_function_start_sal_1 (CORE_ADDR func_addr, obj_section *section,
3560 bool funfirstline)
aab2f208 3561{
42ddae10 3562 symtab_and_line sal = find_pc_sect_line (func_addr, section, 0);
aab2f208 3563
6e22494e
JK
3564 if (funfirstline && sal.symtab != NULL
3565 && (COMPUNIT_LOCATIONS_VALID (SYMTAB_COMPUNIT (sal.symtab))
3566 || SYMTAB_LANGUAGE (sal.symtab) == language_asm))
3567 {
42ddae10 3568 struct gdbarch *gdbarch = get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
141c5cc4 3569
42ddae10 3570 sal.pc = func_addr;
141c5cc4
JK
3571 if (gdbarch_skip_entrypoint_p (gdbarch))
3572 sal.pc = gdbarch_skip_entrypoint (gdbarch, sal.pc);
6e22494e
JK
3573 return sal;
3574 }
3575
aab2f208 3576 /* We always should have a line for the function start address.
42ddae10 3577 If we don't, something is odd. Create a plain SAL referring
aab2f208
DE
3578 just the PC and hope that skip_prologue_sal (if requested)
3579 can find a line number for after the prologue. */
42ddae10 3580 if (sal.pc < func_addr)
aab2f208 3581 {
51abb421 3582 sal = {};
aab2f208 3583 sal.pspace = current_program_space;
42ddae10 3584 sal.pc = func_addr;
08be3fe3 3585 sal.section = section;
aab2f208
DE
3586 }
3587
3588 if (funfirstline)
3589 skip_prologue_sal (&sal);
3590
3591 return sal;
3592}
3593
42ddae10
PA
3594/* See symtab.h. */
3595
cd2bb709
PA
3596symtab_and_line
3597find_function_start_sal (CORE_ADDR func_addr, obj_section *section,
3598 bool funfirstline)
3599{
3600 symtab_and_line sal
3601 = find_function_start_sal_1 (func_addr, section, funfirstline);
3602
3603 /* find_function_start_sal_1 does a linetable search, so it finds
3604 the symtab and linenumber, but not a symbol. Fill in the
3605 function symbol too. */
3606 sal.symbol = find_pc_sect_containing_function (sal.pc, sal.section);
3607
3608 return sal;
3609}
3610
3611/* See symtab.h. */
3612
42ddae10
PA
3613symtab_and_line
3614find_function_start_sal (symbol *sym, bool funfirstline)
3615{
3616 fixup_symbol_section (sym, NULL);
3617 symtab_and_line sal
2b1ffcfd 3618 = find_function_start_sal_1 (BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym)),
cd2bb709
PA
3619 SYMBOL_OBJ_SECTION (symbol_objfile (sym), sym),
3620 funfirstline);
42ddae10
PA
3621 sal.symbol = sym;
3622 return sal;
3623}
3624
3625
8c7a1ee8
EZ
3626/* Given a function start address FUNC_ADDR and SYMTAB, find the first
3627 address for that function that has an entry in SYMTAB's line info
3628 table. If such an entry cannot be found, return FUNC_ADDR
3629 unaltered. */
eca864fe 3630
70221824 3631static CORE_ADDR
8c7a1ee8
EZ
3632skip_prologue_using_lineinfo (CORE_ADDR func_addr, struct symtab *symtab)
3633{
3634 CORE_ADDR func_start, func_end;
3635 struct linetable *l;
952a6d41 3636 int i;
8c7a1ee8
EZ
3637
3638 /* Give up if this symbol has no lineinfo table. */
8435453b 3639 l = SYMTAB_LINETABLE (symtab);
8c7a1ee8
EZ
3640 if (l == NULL)
3641 return func_addr;
3642
3643 /* Get the range for the function's PC values, or give up if we
3644 cannot, for some reason. */
3645 if (!find_pc_partial_function (func_addr, NULL, &func_start, &func_end))
3646 return func_addr;
3647
3648 /* Linetable entries are ordered by PC values, see the commentary in
3649 symtab.h where `struct linetable' is defined. Thus, the first
3650 entry whose PC is in the range [FUNC_START..FUNC_END[ is the
3651 address we are looking for. */
3652 for (i = 0; i < l->nitems; i++)
3653 {
3654 struct linetable_entry *item = &(l->item[i]);
3655
3656 /* Don't use line numbers of zero, they mark special entries in
3657 the table. See the commentary on symtab.h before the
3658 definition of struct linetable. */
3659 if (item->line > 0 && func_start <= item->pc && item->pc < func_end)
3660 return item->pc;
3661 }
3662
3663 return func_addr;
3664}
3665
059acae7
UW
3666/* Adjust SAL to the first instruction past the function prologue.
3667 If the PC was explicitly specified, the SAL is not changed.
5b0e2db4
AB
3668 If the line number was explicitly specified then the SAL can still be
3669 updated, unless the language for SAL is assembler, in which case the SAL
3670 will be left unchanged.
3671 If SAL is already past the prologue, then do nothing. */
eca864fe 3672
059acae7
UW
3673void
3674skip_prologue_sal (struct symtab_and_line *sal)
3675{
3676 struct symbol *sym;
3677 struct symtab_and_line start_sal;
8be455d7 3678 CORE_ADDR pc, saved_pc;
059acae7
UW
3679 struct obj_section *section;
3680 const char *name;
3681 struct objfile *objfile;
3682 struct gdbarch *gdbarch;
3977b71f 3683 const struct block *b, *function_block;
8be455d7 3684 int force_skip, skip;
c906108c 3685
a4b411d6 3686 /* Do not change the SAL if PC was specified explicitly. */
059acae7
UW
3687 if (sal->explicit_pc)
3688 return;
6c95b8df 3689
5b0e2db4
AB
3690 /* In assembly code, if the user asks for a specific line then we should
3691 not adjust the SAL. The user already has instruction level
3692 visibility in this case, so selecting a line other than one requested
3693 is likely to be the wrong choice. */
3694 if (sal->symtab != nullptr
3695 && sal->explicit_line
3696 && SYMTAB_LANGUAGE (sal->symtab) == language_asm)
3697 return;
3698
5ed8105e
PA
3699 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3700
059acae7 3701 switch_to_program_space_and_thread (sal->pspace);
6c95b8df 3702
059acae7
UW
3703 sym = find_pc_sect_function (sal->pc, sal->section);
3704 if (sym != NULL)
bccdca4a 3705 {
059acae7
UW
3706 fixup_symbol_section (sym, NULL);
3707
08be3fe3 3708 objfile = symbol_objfile (sym);
2b1ffcfd 3709 pc = BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym));
08be3fe3 3710 section = SYMBOL_OBJ_SECTION (objfile, sym);
059acae7 3711 name = SYMBOL_LINKAGE_NAME (sym);
c906108c 3712 }
059acae7
UW
3713 else
3714 {
7c7b6655
TT
3715 struct bound_minimal_symbol msymbol
3716 = lookup_minimal_symbol_by_pc_section (sal->pc, sal->section);
433759f7 3717
7c7b6655 3718 if (msymbol.minsym == NULL)
5ed8105e 3719 return;
059acae7 3720
7c7b6655 3721 objfile = msymbol.objfile;
77e371c0 3722 pc = BMSYMBOL_VALUE_ADDRESS (msymbol);
efd66ac6
TT
3723 section = MSYMBOL_OBJ_SECTION (objfile, msymbol.minsym);
3724 name = MSYMBOL_LINKAGE_NAME (msymbol.minsym);
059acae7
UW
3725 }
3726
3727 gdbarch = get_objfile_arch (objfile);
3728
8be455d7
JK
3729 /* Process the prologue in two passes. In the first pass try to skip the
3730 prologue (SKIP is true) and verify there is a real need for it (indicated
3731 by FORCE_SKIP). If no such reason was found run a second pass where the
3732 prologue is not skipped (SKIP is false). */
059acae7 3733
8be455d7
JK
3734 skip = 1;
3735 force_skip = 1;
059acae7 3736
8be455d7
JK
3737 /* Be conservative - allow direct PC (without skipping prologue) only if we
3738 have proven the CU (Compilation Unit) supports it. sal->SYMTAB does not
3739 have to be set by the caller so we use SYM instead. */
08be3fe3
DE
3740 if (sym != NULL
3741 && COMPUNIT_LOCATIONS_VALID (SYMTAB_COMPUNIT (symbol_symtab (sym))))
8be455d7 3742 force_skip = 0;
059acae7 3743
8be455d7
JK
3744 saved_pc = pc;
3745 do
c906108c 3746 {
8be455d7 3747 pc = saved_pc;
4309257c 3748
8be455d7
JK
3749 /* If the function is in an unmapped overlay, use its unmapped LMA address,
3750 so that gdbarch_skip_prologue has something unique to work on. */
3751 if (section_is_overlay (section) && !section_is_mapped (section))
3752 pc = overlay_unmapped_address (pc, section);
3753
3754 /* Skip "first line" of function (which is actually its prologue). */
3755 pc += gdbarch_deprecated_function_start_offset (gdbarch);
591a12a1
UW
3756 if (gdbarch_skip_entrypoint_p (gdbarch))
3757 pc = gdbarch_skip_entrypoint (gdbarch, pc);
8be455d7 3758 if (skip)
46a62268 3759 pc = gdbarch_skip_prologue_noexcept (gdbarch, pc);
8be455d7
JK
3760
3761 /* For overlays, map pc back into its mapped VMA range. */
3762 pc = overlay_mapped_address (pc, section);
3763
3764 /* Calculate line number. */
059acae7 3765 start_sal = find_pc_sect_line (pc, section, 0);
8be455d7
JK
3766
3767 /* Check if gdbarch_skip_prologue left us in mid-line, and the next
3768 line is still part of the same function. */
3769 if (skip && start_sal.pc != pc
2b1ffcfd 3770 && (sym ? (BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym)) <= start_sal.end
b1d96efd 3771 && start_sal.end < BLOCK_END (SYMBOL_BLOCK_VALUE (sym)))
7cbd4a93
TT
3772 : (lookup_minimal_symbol_by_pc_section (start_sal.end, section).minsym
3773 == lookup_minimal_symbol_by_pc_section (pc, section).minsym)))
8be455d7
JK
3774 {
3775 /* First pc of next line */
3776 pc = start_sal.end;
3777 /* Recalculate the line number (might not be N+1). */
3778 start_sal = find_pc_sect_line (pc, section, 0);
3779 }
3780
3781 /* On targets with executable formats that don't have a concept of
3782 constructors (ELF with .init has, PE doesn't), gcc emits a call
3783 to `__main' in `main' between the prologue and before user
3784 code. */
3785 if (gdbarch_skip_main_prologue_p (gdbarch)
7ccffd7c 3786 && name && strcmp_iw (name, "main") == 0)
8be455d7
JK
3787 {
3788 pc = gdbarch_skip_main_prologue (gdbarch, pc);
3789 /* Recalculate the line number (might not be N+1). */
3790 start_sal = find_pc_sect_line (pc, section, 0);
3791 force_skip = 1;
3792 }
4309257c 3793 }
8be455d7 3794 while (!force_skip && skip--);
4309257c 3795
8c7a1ee8
EZ
3796 /* If we still don't have a valid source line, try to find the first
3797 PC in the lineinfo table that belongs to the same function. This
3798 happens with COFF debug info, which does not seem to have an
3799 entry in lineinfo table for the code after the prologue which has
3800 no direct relation to source. For example, this was found to be
3801 the case with the DJGPP target using "gcc -gcoff" when the
3802 compiler inserted code after the prologue to make sure the stack
3803 is aligned. */
8be455d7 3804 if (!force_skip && sym && start_sal.symtab == NULL)
8c7a1ee8 3805 {
08be3fe3 3806 pc = skip_prologue_using_lineinfo (pc, symbol_symtab (sym));
8c7a1ee8 3807 /* Recalculate the line number. */
059acae7 3808 start_sal = find_pc_sect_line (pc, section, 0);
8c7a1ee8
EZ
3809 }
3810
059acae7
UW
3811 /* If we're already past the prologue, leave SAL unchanged. Otherwise
3812 forward SAL to the end of the prologue. */
3813 if (sal->pc >= pc)
3814 return;
3815
3816 sal->pc = pc;
3817 sal->section = section;
059acae7
UW
3818 sal->symtab = start_sal.symtab;
3819 sal->line = start_sal.line;
3820 sal->end = start_sal.end;
c906108c 3821
edb3359d
DJ
3822 /* Check if we are now inside an inlined function. If we can,
3823 use the call site of the function instead. */
059acae7 3824 b = block_for_pc_sect (sal->pc, sal->section);
edb3359d
DJ
3825 function_block = NULL;
3826 while (b != NULL)
3827 {
3828 if (BLOCK_FUNCTION (b) != NULL && block_inlined_p (b))
3829 function_block = b;
3830 else if (BLOCK_FUNCTION (b) != NULL)
3831 break;
3832 b = BLOCK_SUPERBLOCK (b);
3833 }
3834 if (function_block != NULL
3835 && SYMBOL_LINE (BLOCK_FUNCTION (function_block)) != 0)
3836 {
059acae7 3837 sal->line = SYMBOL_LINE (BLOCK_FUNCTION (function_block));
08be3fe3 3838 sal->symtab = symbol_symtab (BLOCK_FUNCTION (function_block));
edb3359d 3839 }
c906108c 3840}
50641945 3841
f1f58506
DE
3842/* Given PC at the function's start address, attempt to find the
3843 prologue end using SAL information. Return zero if the skip fails.
3844
3845 A non-optimized prologue traditionally has one SAL for the function
3846 and a second for the function body. A single line function has
3847 them both pointing at the same line.
3848
3849 An optimized prologue is similar but the prologue may contain
3850 instructions (SALs) from the instruction body. Need to skip those
3851 while not getting into the function body.
3852
3853 The functions end point and an increasing SAL line are used as
3854 indicators of the prologue's endpoint.
3855
3856 This code is based on the function refine_prologue_limit
3857 (found in ia64). */
3858
3859CORE_ADDR
3860skip_prologue_using_sal (struct gdbarch *gdbarch, CORE_ADDR func_addr)
3861{
3862 struct symtab_and_line prologue_sal;
3863 CORE_ADDR start_pc;
3864 CORE_ADDR end_pc;
3865 const struct block *bl;
3866
3867 /* Get an initial range for the function. */
3868 find_pc_partial_function (func_addr, NULL, &start_pc, &end_pc);
3869 start_pc += gdbarch_deprecated_function_start_offset (gdbarch);
3870
3871 prologue_sal = find_pc_line (start_pc, 0);
3872 if (prologue_sal.line != 0)
3873 {
3874 /* For languages other than assembly, treat two consecutive line
3875 entries at the same address as a zero-instruction prologue.
3876 The GNU assembler emits separate line notes for each instruction
3877 in a multi-instruction macro, but compilers generally will not
3878 do this. */
3879 if (prologue_sal.symtab->language != language_asm)
3880 {
8435453b 3881 struct linetable *linetable = SYMTAB_LINETABLE (prologue_sal.symtab);
f1f58506
DE
3882 int idx = 0;
3883
3884 /* Skip any earlier lines, and any end-of-sequence marker
3885 from a previous function. */
3886 while (linetable->item[idx].pc != prologue_sal.pc
3887 || linetable->item[idx].line == 0)
3888 idx++;
3889
3890 if (idx+1 < linetable->nitems
3891 && linetable->item[idx+1].line != 0
3892 && linetable->item[idx+1].pc == start_pc)
3893 return start_pc;
3894 }
3895
3896 /* If there is only one sal that covers the entire function,
3897 then it is probably a single line function, like
3898 "foo(){}". */
3899 if (prologue_sal.end >= end_pc)
3900 return 0;
3901
3902 while (prologue_sal.end < end_pc)
3903 {
3904 struct symtab_and_line sal;
3905
3906 sal = find_pc_line (prologue_sal.end, 0);
3907 if (sal.line == 0)
3908 break;
3909 /* Assume that a consecutive SAL for the same (or larger)
3910 line mark the prologue -> body transition. */
3911 if (sal.line >= prologue_sal.line)
3912 break;
3913 /* Likewise if we are in a different symtab altogether
3914 (e.g. within a file included via #include).  */
3915 if (sal.symtab != prologue_sal.symtab)
3916 break;
3917
3918 /* The line number is smaller. Check that it's from the
3919 same function, not something inlined. If it's inlined,
3920 then there is no point comparing the line numbers. */
3921 bl = block_for_pc (prologue_sal.end);
3922 while (bl)
3923 {
3924 if (block_inlined_p (bl))
3925 break;
3926 if (BLOCK_FUNCTION (bl))
3927 {
3928 bl = NULL;
3929 break;
3930 }
3931 bl = BLOCK_SUPERBLOCK (bl);
3932 }
3933 if (bl != NULL)
3934 break;
3935
3936 /* The case in which compiler's optimizer/scheduler has
3937 moved instructions into the prologue. We look ahead in
3938 the function looking for address ranges whose
3939 corresponding line number is less the first one that we
3940 found for the function. This is more conservative then
3941 refine_prologue_limit which scans a large number of SALs
3942 looking for any in the prologue. */
3943 prologue_sal = sal;
3944 }
3945 }
3946
3947 if (prologue_sal.end < end_pc)
3948 /* Return the end of this line, or zero if we could not find a
3949 line. */
3950 return prologue_sal.end;
3951 else
3952 /* Don't return END_PC, which is past the end of the function. */
3953 return prologue_sal.pc;
3954}
bf223d3e
PA
3955
3956/* See symtab.h. */
3957
3958symbol *
3959find_function_alias_target (bound_minimal_symbol msymbol)
3960{
4024cf2b
PA
3961 CORE_ADDR func_addr;
3962 if (!msymbol_is_function (msymbol.objfile, msymbol.minsym, &func_addr))
bf223d3e
PA
3963 return NULL;
3964
4024cf2b 3965 symbol *sym = find_pc_function (func_addr);
bf223d3e
PA
3966 if (sym != NULL
3967 && SYMBOL_CLASS (sym) == LOC_BLOCK
2b1ffcfd 3968 && BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym)) == func_addr)
bf223d3e
PA
3969 return sym;
3970
3971 return NULL;
3972}
3973
f1f58506 3974\f
c906108c
SS
3975/* If P is of the form "operator[ \t]+..." where `...' is
3976 some legitimate operator text, return a pointer to the
3977 beginning of the substring of the operator text.
3978 Otherwise, return "". */
eca864fe 3979
96142726
TT
3980static const char *
3981operator_chars (const char *p, const char **end)
c906108c
SS
3982{
3983 *end = "";
8090b426 3984 if (!startswith (p, CP_OPERATOR_STR))
c906108c 3985 return *end;
8090b426 3986 p += CP_OPERATOR_LEN;
c906108c
SS
3987
3988 /* Don't get faked out by `operator' being part of a longer
3989 identifier. */
c5aa993b 3990 if (isalpha (*p) || *p == '_' || *p == '$' || *p == '\0')
c906108c
SS
3991 return *end;
3992
3993 /* Allow some whitespace between `operator' and the operator symbol. */
3994 while (*p == ' ' || *p == '\t')
3995 p++;
3996
c378eb4e 3997 /* Recognize 'operator TYPENAME'. */
c906108c 3998
c5aa993b 3999 if (isalpha (*p) || *p == '_' || *p == '$')
c906108c 4000 {
96142726 4001 const char *q = p + 1;
433759f7 4002
c5aa993b 4003 while (isalnum (*q) || *q == '_' || *q == '$')
c906108c
SS
4004 q++;
4005 *end = q;
4006 return p;
4007 }
4008
53e8ad3d
MS
4009 while (*p)
4010 switch (*p)
4011 {
4012 case '\\': /* regexp quoting */
4013 if (p[1] == '*')
4014 {
3e43a32a 4015 if (p[2] == '=') /* 'operator\*=' */
53e8ad3d
MS
4016 *end = p + 3;
4017 else /* 'operator\*' */
4018 *end = p + 2;
4019 return p;
4020 }
4021 else if (p[1] == '[')
4022 {
4023 if (p[2] == ']')
3e43a32a
MS
4024 error (_("mismatched quoting on brackets, "
4025 "try 'operator\\[\\]'"));
53e8ad3d
MS
4026 else if (p[2] == '\\' && p[3] == ']')
4027 {
4028 *end = p + 4; /* 'operator\[\]' */
4029 return p;
4030 }
4031 else
8a3fe4f8 4032 error (_("nothing is allowed between '[' and ']'"));
53e8ad3d 4033 }
9af17804 4034 else
53e8ad3d 4035 {
85102364 4036 /* Gratuitous quote: skip it and move on. */
53e8ad3d
MS
4037 p++;
4038 continue;
4039 }
4040 break;
4041 case '!':
4042 case '=':
4043 case '*':
4044 case '/':
4045 case '%':
4046 case '^':
4047 if (p[1] == '=')
4048 *end = p + 2;
4049 else
4050 *end = p + 1;
4051 return p;
4052 case '<':
4053 case '>':
4054 case '+':
4055 case '-':
4056 case '&':
4057 case '|':
4058 if (p[0] == '-' && p[1] == '>')
4059 {
c378eb4e 4060 /* Struct pointer member operator 'operator->'. */
53e8ad3d
MS
4061 if (p[2] == '*')
4062 {
4063 *end = p + 3; /* 'operator->*' */
4064 return p;
4065 }
4066 else if (p[2] == '\\')
4067 {
4068 *end = p + 4; /* Hopefully 'operator->\*' */
4069 return p;
4070 }
4071 else
4072 {
4073 *end = p + 2; /* 'operator->' */
4074 return p;
4075 }
4076 }
4077 if (p[1] == '=' || p[1] == p[0])
4078 *end = p + 2;
4079 else
4080 *end = p + 1;
4081 return p;
4082 case '~':
4083 case ',':
c5aa993b 4084 *end = p + 1;
53e8ad3d
MS
4085 return p;
4086 case '(':
4087 if (p[1] != ')')
3e43a32a
MS
4088 error (_("`operator ()' must be specified "
4089 "without whitespace in `()'"));
c5aa993b 4090 *end = p + 2;
53e8ad3d
MS
4091 return p;
4092 case '?':
4093 if (p[1] != ':')
3e43a32a
MS
4094 error (_("`operator ?:' must be specified "
4095 "without whitespace in `?:'"));
53e8ad3d
MS
4096 *end = p + 2;
4097 return p;
4098 case '[':
4099 if (p[1] != ']')
3e43a32a
MS
4100 error (_("`operator []' must be specified "
4101 "without whitespace in `[]'"));
53e8ad3d
MS
4102 *end = p + 2;
4103 return p;
4104 default:
8a3fe4f8 4105 error (_("`operator %s' not supported"), p);
53e8ad3d
MS
4106 break;
4107 }
4108
c906108c
SS
4109 *end = "";
4110 return *end;
4111}
c906108c 4112\f
c5aa993b 4113
28cd9371
PW
4114/* What part to match in a file name. */
4115
4116struct filename_partial_match_opts
4117{
4118 /* Only match the directory name part. */
491144b5 4119 bool dirname = false;
28cd9371
PW
4120
4121 /* Only match the basename part. */
491144b5 4122 bool basename = false;
28cd9371
PW
4123};
4124
9fdc877b
DE
4125/* Data structure to maintain printing state for output_source_filename. */
4126
4127struct output_source_filename_data
4128{
28cd9371
PW
4129 /* Output only filenames matching REGEXP. */
4130 std::string regexp;
4131 gdb::optional<compiled_regex> c_regexp;
4132 /* Possibly only match a part of the filename. */
4133 filename_partial_match_opts partial_match;
4134
4135
9fdc877b
DE
4136 /* Cache of what we've seen so far. */
4137 struct filename_seen_cache *filename_seen_cache;
4138
4139 /* Flag of whether we're printing the first one. */
4140 int first;
4141};
4142
c94fdfd0 4143/* Slave routine for sources_info. Force line breaks at ,'s.
9fdc877b
DE
4144 NAME is the name to print.
4145 DATA contains the state for printing and watching for duplicates. */
eca864fe 4146
c94fdfd0 4147static void
9fdc877b
DE
4148output_source_filename (const char *name,
4149 struct output_source_filename_data *data)
c94fdfd0
EZ
4150{
4151 /* Since a single source file can result in several partial symbol
4152 tables, we need to avoid printing it more than once. Note: if
4153 some of the psymtabs are read in and some are not, it gets
4154 printed both under "Source files for which symbols have been
4155 read" and "Source files for which symbols will be read in on
4156 demand". I consider this a reasonable way to deal with the
4157 situation. I'm not sure whether this can also happen for
4158 symtabs; it doesn't hurt to check. */
4159
4160 /* Was NAME already seen? */
bbf2f4df 4161 if (data->filename_seen_cache->seen (name))
c94fdfd0
EZ
4162 {
4163 /* Yes; don't print it again. */
4164 return;
4165 }
9fdc877b 4166
28cd9371
PW
4167 /* Does it match data->regexp? */
4168 if (data->c_regexp.has_value ())
4169 {
4170 const char *to_match;
4171 std::string dirname;
4172
4173 if (data->partial_match.dirname)
4174 {
4175 dirname = ldirname (name);
4176 to_match = dirname.c_str ();
4177 }
4178 else if (data->partial_match.basename)
4179 to_match = lbasename (name);
4180 else
4181 to_match = name;
4182
4183 if (data->c_regexp->exec (to_match, 0, NULL, 0) != 0)
4184 return;
4185 }
4186
4187 /* Print it and reset *FIRST. */
9fdc877b
DE
4188 if (! data->first)
4189 printf_filtered (", ");
4190 data->first = 0;
c906108c
SS
4191
4192 wrap_here ("");
1ed9f74e 4193 fputs_styled (name, file_name_style.style (), gdb_stdout);
c5aa993b 4194}
c906108c 4195
ccefe4c4 4196/* A callback for map_partial_symbol_filenames. */
eca864fe 4197
ccefe4c4 4198static void
533a737e 4199output_partial_symbol_filename (const char *filename, const char *fullname,
ccefe4c4
TT
4200 void *data)
4201{
19ba03f4
SM
4202 output_source_filename (fullname ? fullname : filename,
4203 (struct output_source_filename_data *) data);
ccefe4c4
TT
4204}
4205
28cd9371
PW
4206using isrc_flag_option_def
4207 = gdb::option::flag_option_def<filename_partial_match_opts>;
4208
4209static const gdb::option::option_def info_sources_option_defs[] = {
4210
4211 isrc_flag_option_def {
4212 "dirname",
4213 [] (filename_partial_match_opts *opts) { return &opts->dirname; },
4214 N_("Show only the files having a dirname matching REGEXP."),
4215 },
4216
4217 isrc_flag_option_def {
4218 "basename",
4219 [] (filename_partial_match_opts *opts) { return &opts->basename; },
4220 N_("Show only the files having a basename matching REGEXP."),
4221 },
4222
4223};
4224
4225/* Create an option_def_group for the "info sources" options, with
4226 ISRC_OPTS as context. */
4227
4228static inline gdb::option::option_def_group
4229make_info_sources_options_def_group (filename_partial_match_opts *isrc_opts)
4230{
4231 return {{info_sources_option_defs}, isrc_opts};
4232}
4233
4234/* Prints the header message for the source files that will be printed
4235 with the matching info present in DATA. SYMBOL_MSG is a message
4236 that tells what will or has been done with the symbols of the
4237 matching source files. */
4238
c906108c 4239static void
28cd9371
PW
4240print_info_sources_header (const char *symbol_msg,
4241 const struct output_source_filename_data *data)
4242{
4243 puts_filtered (symbol_msg);
4244 if (!data->regexp.empty ())
4245 {
4246 if (data->partial_match.dirname)
4247 printf_filtered (_("(dirname matching regular expression \"%s\")"),
4248 data->regexp.c_str ());
4249 else if (data->partial_match.basename)
4250 printf_filtered (_("(basename matching regular expression \"%s\")"),
4251 data->regexp.c_str ());
4252 else
4253 printf_filtered (_("(filename matching regular expression \"%s\")"),
4254 data->regexp.c_str ());
4255 }
4256 puts_filtered ("\n");
4257}
4258
4259/* Completer for "info sources". */
4260
4261static void
4262info_sources_command_completer (cmd_list_element *ignore,
4263 completion_tracker &tracker,
4264 const char *text, const char *word)
4265{
4266 const auto group = make_info_sources_options_def_group (nullptr);
4267 if (gdb::option::complete_options
4268 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
4269 return;
4270}
4271
4272static void
4273info_sources_command (const char *args, int from_tty)
c906108c 4274{
9fdc877b 4275 struct output_source_filename_data data;
c5aa993b 4276
c906108c
SS
4277 if (!have_full_symbols () && !have_partial_symbols ())
4278 {
8a3fe4f8 4279 error (_("No symbol table is loaded. Use the \"file\" command."));
c906108c 4280 }
c5aa993b 4281
bbf2f4df
PA
4282 filename_seen_cache filenames_seen;
4283
28cd9371
PW
4284 auto group = make_info_sources_options_def_group (&data.partial_match);
4285
4286 gdb::option::process_options
4287 (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
9fdc877b 4288
28cd9371
PW
4289 if (args != NULL && *args != '\000')
4290 data.regexp = args;
c906108c 4291
28cd9371 4292 data.filename_seen_cache = &filenames_seen;
9fdc877b 4293 data.first = 1;
28cd9371
PW
4294
4295 if (data.partial_match.dirname && data.partial_match.basename)
4296 error (_("You cannot give both -basename and -dirname to 'info sources'."));
4297 if ((data.partial_match.dirname || data.partial_match.basename)
4298 && data.regexp.empty ())
4299 error (_("Missing REGEXP for 'info sources'."));
4300
4301 if (data.regexp.empty ())
4302 data.c_regexp.reset ();
4303 else
4304 {
4305 int cflags = REG_NOSUB;
4306#ifdef HAVE_CASE_INSENSITIVE_FILE_SYSTEM
4307 cflags |= REG_ICASE;
4308#endif
4309 data.c_regexp.emplace (data.regexp.c_str (), cflags,
4310 _("Invalid regexp"));
4311 }
4312
4313 print_info_sources_header
4314 (_("Source files for which symbols have been read in:\n"), &data);
4315
2030c079 4316 for (objfile *objfile : current_program_space->objfiles ())
8b31193a 4317 {
b669c953 4318 for (compunit_symtab *cu : objfile->compunits ())
8b31193a
TT
4319 {
4320 for (symtab *s : compunit_filetabs (cu))
4321 {
4322 const char *fullname = symtab_to_fullname (s);
433759f7 4323
8b31193a
TT
4324 output_source_filename (fullname, &data);
4325 }
4326 }
4327 }
c906108c 4328 printf_filtered ("\n\n");
c5aa993b 4329
28cd9371
PW
4330 print_info_sources_header
4331 (_("Source files for which symbols will be read in on demand:\n"), &data);
c906108c 4332
bbf2f4df 4333 filenames_seen.clear ();
9fdc877b 4334 data.first = 1;
bb4142cf
DE
4335 map_symbol_filenames (output_partial_symbol_filename, &data,
4336 1 /*need_fullname*/);
c906108c
SS
4337 printf_filtered ("\n");
4338}
4339
fbd9ab74
JK
4340/* Compare FILE against all the NFILES entries of FILES. If BASENAMES is
4341 non-zero compare only lbasename of FILES. */
4342
c906108c 4343static int
96142726 4344file_matches (const char *file, const char *files[], int nfiles, int basenames)
c906108c
SS
4345{
4346 int i;
4347
4348 if (file != NULL && nfiles != 0)
4349 {
4350 for (i = 0; i < nfiles; i++)
c5aa993b 4351 {
fbd9ab74
JK
4352 if (compare_filenames_for_search (file, (basenames
4353 ? lbasename (files[i])
4354 : files[i])))
c5aa993b
JM
4355 return 1;
4356 }
c906108c
SS
4357 }
4358 else if (nfiles == 0)
4359 return 1;
4360 return 0;
4361}
4362
b52109bc 4363/* Helper function for sort_search_symbols_remove_dups and qsort. Can only
434d2d4f 4364 sort symbols, not minimal symbols. */
eca864fe 4365
b9c04fb2
TT
4366int
4367symbol_search::compare_search_syms (const symbol_search &sym_a,
4368 const symbol_search &sym_b)
434d2d4f 4369{
b52109bc
DE
4370 int c;
4371
b9c04fb2
TT
4372 c = FILENAME_CMP (symbol_symtab (sym_a.symbol)->filename,
4373 symbol_symtab (sym_b.symbol)->filename);
b52109bc
DE
4374 if (c != 0)
4375 return c;
434d2d4f 4376
b9c04fb2
TT
4377 if (sym_a.block != sym_b.block)
4378 return sym_a.block - sym_b.block;
b52109bc 4379
b9c04fb2
TT
4380 return strcmp (SYMBOL_PRINT_NAME (sym_a.symbol),
4381 SYMBOL_PRINT_NAME (sym_b.symbol));
434d2d4f
DJ
4382}
4383
12615cba
PW
4384/* Returns true if the type_name of symbol_type of SYM matches TREG.
4385 If SYM has no symbol_type or symbol_name, returns false. */
4386
4387bool
4388treg_matches_sym_type_name (const compiled_regex &treg,
4389 const struct symbol *sym)
4390{
4391 struct type *sym_type;
4392 std::string printed_sym_type_name;
4393
4394 if (symbol_lookup_debug > 1)
4395 {
4396 fprintf_unfiltered (gdb_stdlog,
4397 "treg_matches_sym_type_name\n sym %s\n",
4398 SYMBOL_NATURAL_NAME (sym));
4399 }
4400
4401 sym_type = SYMBOL_TYPE (sym);
4402 if (sym_type == NULL)
4403 return false;
4404
43d397ca
PW
4405 {
4406 scoped_switch_to_sym_language_if_auto l (sym);
12615cba 4407
12615cba 4408 printed_sym_type_name = type_to_string (sym_type);
43d397ca
PW
4409 }
4410
12615cba
PW
4411
4412 if (symbol_lookup_debug > 1)
4413 {
4414 fprintf_unfiltered (gdb_stdlog,
4415 " sym_type_name %s\n",
4416 printed_sym_type_name.c_str ());
4417 }
4418
4419
4420 if (printed_sym_type_name.empty ())
4421 return false;
4422
4423 return treg.exec (printed_sym_type_name.c_str (), 0, NULL, 0) == 0;
4424}
4425
4426
b9c04fb2 4427/* Sort the symbols in RESULT and remove duplicates. */
b52109bc
DE
4428
4429static void
b9c04fb2 4430sort_search_symbols_remove_dups (std::vector<symbol_search> *result)
434d2d4f 4431{
b9c04fb2
TT
4432 std::sort (result->begin (), result->end ());
4433 result->erase (std::unique (result->begin (), result->end ()),
4434 result->end ());
434d2d4f 4435}
5bd98722 4436
c906108c 4437/* Search the symbol table for matches to the regular expression REGEXP,
b9c04fb2 4438 returning the results.
c906108c
SS
4439
4440 Only symbols of KIND are searched:
e8930875 4441 VARIABLES_DOMAIN - search all symbols, excluding functions, type names,
12615cba
PW
4442 and constants (enums).
4443 if T_REGEXP is not NULL, only returns var that have
4444 a type matching regular expression T_REGEXP.
176620f1
EZ
4445 FUNCTIONS_DOMAIN - search all functions
4446 TYPES_DOMAIN - search all type names
7b08b9eb 4447 ALL_DOMAIN - an internal error for this function
c906108c 4448
b52109bc
DE
4449 Within each file the results are sorted locally; each symtab's global and
4450 static blocks are separately alphabetized.
4acfdd20
AB
4451 Duplicate entries are removed.
4452
4453 When EXCLUDE_MINSYMS is false then matching minsyms are also returned,
4454 otherwise they are excluded. */
c378eb4e 4455
b9c04fb2 4456std::vector<symbol_search>
96142726 4457search_symbols (const char *regexp, enum search_domain kind,
12615cba 4458 const char *t_regexp,
4acfdd20
AB
4459 int nfiles, const char *files[],
4460 bool exclude_minsyms)
c906108c 4461{
346d1dfe 4462 const struct blockvector *bv;
582942f4 4463 const struct block *b;
52f0bd74 4464 int i = 0;
8157b174 4465 struct block_iterator iter;
52f0bd74 4466 struct symbol *sym;
c906108c 4467 int found_misc = 0;
bc043ef3 4468 static const enum minimal_symbol_type types[]
eb86c5e2 4469 = {mst_data, mst_text, mst_unknown};
bc043ef3 4470 static const enum minimal_symbol_type types2[]
eb86c5e2 4471 = {mst_bss, mst_file_text, mst_unknown};
bc043ef3 4472 static const enum minimal_symbol_type types3[]
eb86c5e2 4473 = {mst_file_data, mst_solib_trampoline, mst_unknown};
bc043ef3 4474 static const enum minimal_symbol_type types4[]
eb86c5e2 4475 = {mst_file_bss, mst_text_gnu_ifunc, mst_unknown};
c906108c
SS
4476 enum minimal_symbol_type ourtype;
4477 enum minimal_symbol_type ourtype2;
4478 enum minimal_symbol_type ourtype3;
4479 enum minimal_symbol_type ourtype4;
b9c04fb2 4480 std::vector<symbol_search> result;
2d7cc5c7 4481 gdb::optional<compiled_regex> preg;
12615cba 4482 gdb::optional<compiled_regex> treg;
c906108c 4483
e8930875
JK
4484 gdb_assert (kind <= TYPES_DOMAIN);
4485
8903c50d
TT
4486 ourtype = types[kind];
4487 ourtype2 = types2[kind];
4488 ourtype3 = types3[kind];
4489 ourtype4 = types4[kind];
c906108c 4490
c906108c
SS
4491 if (regexp != NULL)
4492 {
4493 /* Make sure spacing is right for C++ operators.
4494 This is just a courtesy to make the matching less sensitive
4495 to how many spaces the user leaves between 'operator'
c378eb4e 4496 and <TYPENAME> or <OPERATOR>. */
96142726
TT
4497 const char *opend;
4498 const char *opname = operator_chars (regexp, &opend);
433759f7 4499
c906108c 4500 if (*opname)
c5aa993b 4501 {
3e43a32a
MS
4502 int fix = -1; /* -1 means ok; otherwise number of
4503 spaces needed. */
433759f7 4504
c5aa993b
JM
4505 if (isalpha (*opname) || *opname == '_' || *opname == '$')
4506 {
c378eb4e 4507 /* There should 1 space between 'operator' and 'TYPENAME'. */
c5aa993b
JM
4508 if (opname[-1] != ' ' || opname[-2] == ' ')
4509 fix = 1;
4510 }
4511 else
4512 {
c378eb4e 4513 /* There should 0 spaces between 'operator' and 'OPERATOR'. */
c5aa993b
JM
4514 if (opname[-1] == ' ')
4515 fix = 0;
4516 }
c378eb4e 4517 /* If wrong number of spaces, fix it. */
c5aa993b
JM
4518 if (fix >= 0)
4519 {
045f55a6 4520 char *tmp = (char *) alloca (8 + fix + strlen (opname) + 1);
433759f7 4521
c5aa993b
JM
4522 sprintf (tmp, "operator%.*s%s", fix, " ", opname);
4523 regexp = tmp;
4524 }
4525 }
4526
2d7cc5c7
PA
4527 int cflags = REG_NOSUB | (case_sensitivity == case_sensitive_off
4528 ? REG_ICASE : 0);
4529 preg.emplace (regexp, cflags, _("Invalid regexp"));
c906108c
SS
4530 }
4531
12615cba
PW
4532 if (t_regexp != NULL)
4533 {
4534 int cflags = REG_NOSUB | (case_sensitivity == case_sensitive_off
4535 ? REG_ICASE : 0);
4536 treg.emplace (t_regexp, cflags, _("Invalid regexp"));
4537 }
4538
c906108c
SS
4539 /* Search through the partial symtabs *first* for all symbols
4540 matching the regexp. That way we don't have to reproduce all of
c378eb4e 4541 the machinery below. */
14bc53a8
PA
4542 expand_symtabs_matching ([&] (const char *filename, bool basenames)
4543 {
4544 return file_matches (filename, files, nfiles,
4545 basenames);
4546 },
b5ec771e 4547 lookup_name_info::match_any (),
14bc53a8
PA
4548 [&] (const char *symname)
4549 {
12615cba
PW
4550 return (!preg.has_value ()
4551 || preg->exec (symname,
4552 0, NULL, 0) == 0);
14bc53a8
PA
4553 },
4554 NULL,
4555 kind);
c906108c
SS
4556
4557 /* Here, we search through the minimal symbol tables for functions
4558 and variables that match, and force their symbols to be read.
4559 This is in particular necessary for demangled variable names,
4560 which are no longer put into the partial symbol tables.
4561 The symbol will then be found during the scan of symtabs below.
4562
4563 For functions, find_pc_symtab should succeed if we have debug info
422d65e7
DE
4564 for the function, for variables we have to call
4565 lookup_symbol_in_objfile_from_linkage_name to determine if the variable
4566 has debug info.
c906108c 4567 If the lookup fails, set found_misc so that we will rescan to print
422d65e7
DE
4568 any matching symbols without debug info.
4569 We only search the objfile the msymbol came from, we no longer search
4570 all objfiles. In large programs (1000s of shared libs) searching all
4571 objfiles is not worth the pain. */
c906108c 4572
176620f1 4573 if (nfiles == 0 && (kind == VARIABLES_DOMAIN || kind == FUNCTIONS_DOMAIN))
c906108c 4574 {
2030c079 4575 for (objfile *objfile : current_program_space->objfiles ())
5325b9bf 4576 {
7932255d 4577 for (minimal_symbol *msymbol : objfile->msymbols ())
5325b9bf
TT
4578 {
4579 QUIT;
89295b4d 4580
5325b9bf
TT
4581 if (msymbol->created_by_gdb)
4582 continue;
422d65e7 4583
5325b9bf
TT
4584 if (MSYMBOL_TYPE (msymbol) == ourtype
4585 || MSYMBOL_TYPE (msymbol) == ourtype2
4586 || MSYMBOL_TYPE (msymbol) == ourtype3
4587 || MSYMBOL_TYPE (msymbol) == ourtype4)
4588 {
4589 if (!preg.has_value ()
4590 || preg->exec (MSYMBOL_NATURAL_NAME (msymbol), 0,
4591 NULL, 0) == 0)
4592 {
4593 /* Note: An important side-effect of these
4594 lookup functions is to expand the symbol
4595 table if msymbol is found, for the benefit of
d8aeb77f 4596 the next loop on compunits. */
5325b9bf
TT
4597 if (kind == FUNCTIONS_DOMAIN
4598 ? (find_pc_compunit_symtab
4599 (MSYMBOL_VALUE_ADDRESS (objfile, msymbol))
4600 == NULL)
4601 : (lookup_symbol_in_objfile_from_linkage_name
4602 (objfile, MSYMBOL_LINKAGE_NAME (msymbol),
4603 VAR_DOMAIN)
4604 .symbol == NULL))
4605 found_misc = 1;
4606 }
4607 }
4608 }
4609 }
c906108c
SS
4610 }
4611
2030c079 4612 for (objfile *objfile : current_program_space->objfiles ())
d8aeb77f 4613 {
b669c953 4614 for (compunit_symtab *cust : objfile->compunits ())
d8aeb77f
TT
4615 {
4616 bv = COMPUNIT_BLOCKVECTOR (cust);
4617 for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
4618 {
4619 b = BLOCKVECTOR_BLOCK (bv, i);
4620 ALL_BLOCK_SYMBOLS (b, iter, sym)
4621 {
4622 struct symtab *real_symtab = symbol_symtab (sym);
4623
4624 QUIT;
4625
4626 /* Check first sole REAL_SYMTAB->FILENAME. It does
4627 not need to be a substring of symtab_to_fullname as
4628 it may contain "./" etc. */
4629 if ((file_matches (real_symtab->filename, files, nfiles, 0)
4630 || ((basenames_may_differ
4631 || file_matches (lbasename (real_symtab->filename),
4632 files, nfiles, 1))
4633 && file_matches (symtab_to_fullname (real_symtab),
4634 files, nfiles, 0)))
4635 && ((!preg.has_value ()
4636 || preg->exec (SYMBOL_NATURAL_NAME (sym), 0,
4637 NULL, 0) == 0)
4638 && ((kind == VARIABLES_DOMAIN
4639 && SYMBOL_CLASS (sym) != LOC_TYPEDEF
4640 && SYMBOL_CLASS (sym) != LOC_UNRESOLVED
4641 && SYMBOL_CLASS (sym) != LOC_BLOCK
4642 /* LOC_CONST can be used for more than
4643 just enums, e.g., c++ static const
4644 members. We only want to skip enums
4645 here. */
4646 && !(SYMBOL_CLASS (sym) == LOC_CONST
4647 && (TYPE_CODE (SYMBOL_TYPE (sym))
4648 == TYPE_CODE_ENUM))
4649 && (!treg.has_value ()
4650 || treg_matches_sym_type_name (*treg, sym)))
4651 || (kind == FUNCTIONS_DOMAIN
4652 && SYMBOL_CLASS (sym) == LOC_BLOCK
4653 && (!treg.has_value ()
4654 || treg_matches_sym_type_name (*treg,
4655 sym)))
4656 || (kind == TYPES_DOMAIN
d8f27c60
AB
4657 && SYMBOL_CLASS (sym) == LOC_TYPEDEF
4658 && SYMBOL_DOMAIN (sym) != MODULE_DOMAIN))))
d8aeb77f
TT
4659 {
4660 /* match */
4661 result.emplace_back (i, sym);
4662 }
4663 }
4664 }
4665 }
4666 }
c906108c 4667
b9c04fb2
TT
4668 if (!result.empty ())
4669 sort_search_symbols_remove_dups (&result);
b52109bc 4670
c906108c 4671 /* If there are no eyes, avoid all contact. I mean, if there are
a8462bbf
PW
4672 no debug symbols, then add matching minsyms. But if the user wants
4673 to see symbols matching a type regexp, then never give a minimal symbol,
4674 as we assume that a minimal symbol does not have a type. */
c906108c 4675
a8462bbf 4676 if ((found_misc || (nfiles == 0 && kind != FUNCTIONS_DOMAIN))
4acfdd20 4677 && !exclude_minsyms
a8462bbf 4678 && !treg.has_value ())
c906108c 4679 {
2030c079 4680 for (objfile *objfile : current_program_space->objfiles ())
5325b9bf 4681 {
7932255d 4682 for (minimal_symbol *msymbol : objfile->msymbols ())
5325b9bf
TT
4683 {
4684 QUIT;
89295b4d 4685
5325b9bf
TT
4686 if (msymbol->created_by_gdb)
4687 continue;
422d65e7 4688
5325b9bf
TT
4689 if (MSYMBOL_TYPE (msymbol) == ourtype
4690 || MSYMBOL_TYPE (msymbol) == ourtype2
4691 || MSYMBOL_TYPE (msymbol) == ourtype3
4692 || MSYMBOL_TYPE (msymbol) == ourtype4)
4693 {
4694 if (!preg.has_value ()
4695 || preg->exec (MSYMBOL_NATURAL_NAME (msymbol), 0,
4696 NULL, 0) == 0)
4697 {
4698 /* For functions we can do a quick check of whether the
4699 symbol might be found via find_pc_symtab. */
4700 if (kind != FUNCTIONS_DOMAIN
4701 || (find_pc_compunit_symtab
4702 (MSYMBOL_VALUE_ADDRESS (objfile, msymbol))
4703 == NULL))
4704 {
4705 if (lookup_symbol_in_objfile_from_linkage_name
4706 (objfile, MSYMBOL_LINKAGE_NAME (msymbol),
4707 VAR_DOMAIN)
4708 .symbol == NULL)
4709 {
4710 /* match */
4711 result.emplace_back (i, msymbol, objfile);
4712 }
4713 }
4714 }
4715 }
4716 }
4717 }
c906108c
SS
4718 }
4719
b9c04fb2 4720 return result;
c906108c
SS
4721}
4722
4723/* Helper function for symtab_symbol_info, this function uses
4724 the data returned from search_symbols() to print information
c7dcbf88
AA
4725 regarding the match to gdb_stdout. If LAST is not NULL,
4726 print file and line number information for the symbol as
4727 well. Skip printing the filename if it matches LAST. */
c378eb4e 4728
c906108c 4729static void
8903c50d 4730print_symbol_info (enum search_domain kind,
d01060f0 4731 struct symbol *sym,
05cba821 4732 int block, const char *last)
c906108c 4733{
43d397ca 4734 scoped_switch_to_sym_language_if_auto l (sym);
08be3fe3 4735 struct symtab *s = symbol_symtab (sym);
05cba821 4736
c7dcbf88 4737 if (last != NULL)
c906108c 4738 {
c7dcbf88 4739 const char *s_filename = symtab_to_filename_for_display (s);
c906108c 4740
c7dcbf88
AA
4741 if (filename_cmp (last, s_filename) != 0)
4742 {
6a831f06
PA
4743 printf_filtered (_("\nFile %ps:\n"),
4744 styled_string (file_name_style.style (),
4745 s_filename));
c7dcbf88
AA
4746 }
4747
4748 if (SYMBOL_LINE (sym) != 0)
4749 printf_filtered ("%d:\t", SYMBOL_LINE (sym));
4750 else
4751 puts_filtered ("\t");
4752 }
b744723f 4753
176620f1 4754 if (kind != TYPES_DOMAIN && block == STATIC_BLOCK)
c906108c 4755 printf_filtered ("static ");
c5aa993b 4756
c378eb4e 4757 /* Typedef that is not a C++ class. */
176620f1
EZ
4758 if (kind == TYPES_DOMAIN
4759 && SYMBOL_DOMAIN (sym) != STRUCT_DOMAIN)
eb86c5e2
AB
4760 {
4761 /* FIXME: For C (and C++) we end up with a difference in output here
4762 between how a typedef is printed, and non-typedefs are printed.
4763 The TYPEDEF_PRINT code places a ";" at the end in an attempt to
4764 appear C-like, while TYPE_PRINT doesn't.
4765
4766 For the struct printing case below, things are worse, we force
4767 printing of the ";" in this function, which is going to be wrong
4768 for languages that don't require a ";" between statements. */
4769 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_TYPEDEF)
4770 typedef_print (SYMBOL_TYPE (sym), sym, gdb_stdout);
4771 else
4772 {
4773 type_print (SYMBOL_TYPE (sym), "", gdb_stdout, -1);
4774 printf_filtered ("\n");
4775 }
4776 }
c378eb4e 4777 /* variable, func, or typedef-that-is-c++-class. */
d50bd42b
DE
4778 else if (kind < TYPES_DOMAIN
4779 || (kind == TYPES_DOMAIN
4780 && SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN))
c906108c
SS
4781 {
4782 type_print (SYMBOL_TYPE (sym),
c5aa993b 4783 (SYMBOL_CLASS (sym) == LOC_TYPEDEF
de5ad195 4784 ? "" : SYMBOL_PRINT_NAME (sym)),
c5aa993b 4785 gdb_stdout, 0);
c906108c
SS
4786
4787 printf_filtered (";\n");
4788 }
c906108c
SS
4789}
4790
4791/* This help function for symtab_symbol_info() prints information
c378eb4e
MS
4792 for non-debugging symbols to gdb_stdout. */
4793
c906108c 4794static void
7c7b6655 4795print_msymbol_info (struct bound_minimal_symbol msymbol)
c906108c 4796{
7c7b6655 4797 struct gdbarch *gdbarch = get_objfile_arch (msymbol.objfile);
3ac4495a
MS
4798 char *tmp;
4799
d80b854b 4800 if (gdbarch_addr_bit (gdbarch) <= 32)
77e371c0 4801 tmp = hex_string_custom (BMSYMBOL_VALUE_ADDRESS (msymbol)
bb599908
PH
4802 & (CORE_ADDR) 0xffffffff,
4803 8);
3ac4495a 4804 else
77e371c0 4805 tmp = hex_string_custom (BMSYMBOL_VALUE_ADDRESS (msymbol),
bb599908 4806 16);
6a831f06
PA
4807
4808 ui_file_style sym_style = (msymbol.minsym->text_p ()
4809 ? function_name_style.style ()
4810 : ui_file_style ());
4811
4812 printf_filtered (_("%ps %ps\n"),
4813 styled_string (address_style.style (), tmp),
4814 styled_string (sym_style,
4815 MSYMBOL_PRINT_NAME (msymbol.minsym)));
c906108c
SS
4816}
4817
4818/* This is the guts of the commands "info functions", "info types", and
c378eb4e 4819 "info variables". It calls search_symbols to find all matches and then
c906108c 4820 print_[m]symbol_info to print out some useful information about the
c378eb4e
MS
4821 matches. */
4822
c906108c 4823static void
4acfdd20 4824symtab_symbol_info (bool quiet, bool exclude_minsyms,
12615cba
PW
4825 const char *regexp, enum search_domain kind,
4826 const char *t_regexp, int from_tty)
c906108c 4827{
bc043ef3 4828 static const char * const classnames[] =
e8930875 4829 {"variable", "function", "type"};
c7dcbf88 4830 const char *last_filename = "";
c906108c
SS
4831 int first = 1;
4832
e8930875
JK
4833 gdb_assert (kind <= TYPES_DOMAIN);
4834
b16507e0
AB
4835 if (regexp != nullptr && *regexp == '\0')
4836 regexp = nullptr;
4837
c378eb4e 4838 /* Must make sure that if we're interrupted, symbols gets freed. */
12615cba 4839 std::vector<symbol_search> symbols = search_symbols (regexp, kind,
4acfdd20
AB
4840 t_regexp, 0, NULL,
4841 exclude_minsyms);
c906108c 4842
12615cba
PW
4843 if (!quiet)
4844 {
4845 if (regexp != NULL)
4846 {
4847 if (t_regexp != NULL)
4848 printf_filtered
4849 (_("All %ss matching regular expression \"%s\""
0c95f9ed 4850 " with type matching regular expression \"%s\":\n"),
12615cba
PW
4851 classnames[kind], regexp, t_regexp);
4852 else
4853 printf_filtered (_("All %ss matching regular expression \"%s\":\n"),
4854 classnames[kind], regexp);
4855 }
4856 else
4857 {
4858 if (t_regexp != NULL)
4859 printf_filtered
4860 (_("All defined %ss"
0c95f9ed 4861 " with type matching regular expression \"%s\" :\n"),
12615cba
PW
4862 classnames[kind], t_regexp);
4863 else
4864 printf_filtered (_("All defined %ss:\n"), classnames[kind]);
4865 }
4866 }
c906108c 4867
b9c04fb2 4868 for (const symbol_search &p : symbols)
c906108c
SS
4869 {
4870 QUIT;
4871
b9c04fb2 4872 if (p.msymbol.minsym != NULL)
c5aa993b
JM
4873 {
4874 if (first)
4875 {
12615cba
PW
4876 if (!quiet)
4877 printf_filtered (_("\nNon-debugging symbols:\n"));
c5aa993b
JM
4878 first = 0;
4879 }
b9c04fb2 4880 print_msymbol_info (p.msymbol);
c5aa993b 4881 }
c906108c 4882 else
c5aa993b
JM
4883 {
4884 print_symbol_info (kind,
b9c04fb2
TT
4885 p.symbol,
4886 p.block,
c5aa993b 4887 last_filename);
d01060f0 4888 last_filename
b9c04fb2 4889 = symtab_to_filename_for_display (symbol_symtab (p.symbol));
c5aa993b 4890 }
c906108c 4891 }
c906108c
SS
4892}
4893
4acfdd20
AB
4894/* Structure to hold the values of the options used by the 'info variables'
4895 and 'info functions' commands. These correspond to the -q, -t, and -n
4896 options. */
4897
4898struct info_print_options
4899{
491144b5
CB
4900 bool quiet = false;
4901 bool exclude_minsyms = false;
4acfdd20
AB
4902 char *type_regexp = nullptr;
4903
4904 ~info_print_options ()
4905 {
4906 xfree (type_regexp);
4907 }
4908};
4909
4910/* The options used by the 'info variables' and 'info functions'
4911 commands. */
4912
4913static const gdb::option::option_def info_print_options_defs[] = {
4914 gdb::option::boolean_option_def<info_print_options> {
4915 "q",
4916 [] (info_print_options *opt) { return &opt->quiet; },
4917 nullptr, /* show_cmd_cb */
4918 nullptr /* set_doc */
4919 },
4920
4921 gdb::option::boolean_option_def<info_print_options> {
4922 "n",
4923 [] (info_print_options *opt) { return &opt->exclude_minsyms; },
4924 nullptr, /* show_cmd_cb */
4925 nullptr /* set_doc */
4926 },
4927
4928 gdb::option::string_option_def<info_print_options> {
4929 "t",
4930 [] (info_print_options *opt) { return &opt->type_regexp; },
4931 nullptr, /* show_cmd_cb */
4932 nullptr /* set_doc */
4933 }
4934};
4935
4936/* Returns the option group used by 'info variables' and 'info
4937 functions'. */
4938
4939static gdb::option::option_def_group
4940make_info_print_options_def_group (info_print_options *opts)
4941{
4942 return {{info_print_options_defs}, opts};
4943}
4944
4945/* Command completer for 'info variables' and 'info functions'. */
4946
4947static void
4948info_print_command_completer (struct cmd_list_element *ignore,
4949 completion_tracker &tracker,
4950 const char *text, const char * /* word */)
4951{
4952 const auto group
4953 = make_info_print_options_def_group (nullptr);
4954 if (gdb::option::complete_options
4955 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
4956 return;
4957
4958 const char *word = advance_to_expression_complete_word_point (tracker, text);
4959 symbol_completer (ignore, tracker, text, word);
4960}
4961
b16507e0
AB
4962/* Implement the 'info variables' command. */
4963
0b39b52e 4964static void
12615cba 4965info_variables_command (const char *args, int from_tty)
0b39b52e 4966{
b16507e0 4967 info_print_options opts;
4acfdd20
AB
4968 auto grp = make_info_print_options_def_group (&opts);
4969 gdb::option::process_options
4970 (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
4971 if (args != nullptr && *args == '\0')
4972 args = nullptr;
b16507e0 4973
4acfdd20 4974 symtab_symbol_info (opts.quiet, opts.exclude_minsyms, args, VARIABLES_DOMAIN,
b16507e0 4975 opts.type_regexp, from_tty);
0b39b52e
TT
4976}
4977
b16507e0 4978/* Implement the 'info functions' command. */
12615cba 4979
c906108c 4980static void
12615cba 4981info_functions_command (const char *args, int from_tty)
c906108c 4982{
b16507e0 4983 info_print_options opts;
4acfdd20
AB
4984 auto grp = make_info_print_options_def_group (&opts);
4985 gdb::option::process_options
4986 (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
4987 if (args != nullptr && *args == '\0')
4988 args = nullptr;
b16507e0 4989
4acfdd20
AB
4990 symtab_symbol_info (opts.quiet, opts.exclude_minsyms, args,
4991 FUNCTIONS_DOMAIN, opts.type_regexp, from_tty);
c906108c
SS
4992}
4993
a8eab7c6
AB
4994/* Holds the -q option for the 'info types' command. */
4995
4996struct info_types_options
4997{
491144b5 4998 bool quiet = false;
a8eab7c6
AB
4999};
5000
5001/* The options used by the 'info types' command. */
5002
5003static const gdb::option::option_def info_types_options_defs[] = {
5004 gdb::option::boolean_option_def<info_types_options> {
5005 "q",
5006 [] (info_types_options *opt) { return &opt->quiet; },
5007 nullptr, /* show_cmd_cb */
5008 nullptr /* set_doc */
5009 }
5010};
5011
5012/* Returns the option group used by 'info types'. */
5013
5014static gdb::option::option_def_group
5015make_info_types_options_def_group (info_types_options *opts)
5016{
5017 return {{info_types_options_defs}, opts};
5018}
5019
5020/* Implement the 'info types' command. */
357e46e7 5021
c906108c 5022static void
a8eab7c6 5023info_types_command (const char *args, int from_tty)
c906108c 5024{
a8eab7c6
AB
5025 info_types_options opts;
5026
5027 auto grp = make_info_types_options_def_group (&opts);
5028 gdb::option::process_options
5029 (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
5030 if (args != nullptr && *args == '\0')
5031 args = nullptr;
4acfdd20 5032 symtab_symbol_info (opts.quiet, false, args, TYPES_DOMAIN, NULL, from_tty);
a8eab7c6
AB
5033}
5034
5035/* Command completer for 'info types' command. */
5036
5037static void
5038info_types_command_completer (struct cmd_list_element *ignore,
5039 completion_tracker &tracker,
5040 const char *text, const char * /* word */)
5041{
5042 const auto group
5043 = make_info_types_options_def_group (nullptr);
5044 if (gdb::option::complete_options
5045 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
5046 return;
5047
5048 const char *word = advance_to_expression_complete_word_point (tracker, text);
5049 symbol_completer (ignore, tracker, text, word);
c906108c
SS
5050}
5051
c378eb4e 5052/* Breakpoint all functions matching regular expression. */
8926118c 5053
8b93c638 5054void
fba45db2 5055rbreak_command_wrapper (char *regexp, int from_tty)
8b93c638
JM
5056{
5057 rbreak_command (regexp, from_tty);
5058}
8926118c 5059
c906108c 5060static void
0b39b52e 5061rbreak_command (const char *regexp, int from_tty)
c906108c 5062{
c80049d3 5063 std::string string;
96142726
TT
5064 const char **files = NULL;
5065 const char *file_name;
8bd10a10 5066 int nfiles = 0;
c906108c 5067
8bd10a10
CM
5068 if (regexp)
5069 {
0b39b52e 5070 const char *colon = strchr (regexp, ':');
433759f7 5071
8bd10a10
CM
5072 if (colon && *(colon + 1) != ':')
5073 {
5074 int colon_index;
96142726 5075 char *local_name;
8bd10a10
CM
5076
5077 colon_index = colon - regexp;
224c3ddb 5078 local_name = (char *) alloca (colon_index + 1);
96142726
TT
5079 memcpy (local_name, regexp, colon_index);
5080 local_name[colon_index--] = 0;
5081 while (isspace (local_name[colon_index]))
5082 local_name[colon_index--] = 0;
5083 file_name = local_name;
8bd10a10
CM
5084 files = &file_name;
5085 nfiles = 1;
529480d0 5086 regexp = skip_spaces (colon + 1);
8bd10a10
CM
5087 }
5088 }
5089
b9c04fb2
TT
5090 std::vector<symbol_search> symbols = search_symbols (regexp,
5091 FUNCTIONS_DOMAIN,
12615cba 5092 NULL,
4acfdd20
AB
5093 nfiles, files,
5094 false);
c906108c 5095
c80049d3 5096 scoped_rbreak_breakpoints finalize;
b9c04fb2 5097 for (const symbol_search &p : symbols)
c906108c 5098 {
b9c04fb2 5099 if (p.msymbol.minsym == NULL)
c5aa993b 5100 {
b9c04fb2 5101 struct symtab *symtab = symbol_symtab (p.symbol);
d01060f0 5102 const char *fullname = symtab_to_fullname (symtab);
05cba821 5103
c80049d3
TT
5104 string = string_printf ("%s:'%s'", fullname,
5105 SYMBOL_LINKAGE_NAME (p.symbol));
5106 break_command (&string[0], from_tty);
c7dcbf88 5107 print_symbol_info (FUNCTIONS_DOMAIN, p.symbol, p.block, NULL);
c5aa993b 5108 }
c906108c 5109 else
c5aa993b 5110 {
c80049d3
TT
5111 string = string_printf ("'%s'",
5112 MSYMBOL_LINKAGE_NAME (p.msymbol.minsym));
6214f497 5113
c80049d3 5114 break_command (&string[0], from_tty);
c5aa993b 5115 printf_filtered ("<function, no debug info> %s;\n",
b9c04fb2 5116 MSYMBOL_PRINT_NAME (p.msymbol.minsym));
c5aa993b 5117 }
c906108c 5118 }
c906108c 5119}
c906108c 5120\f
c5aa993b 5121
c62446b1 5122/* Evaluate if SYMNAME matches LOOKUP_NAME. */
1976171a
JK
5123
5124static int
c62446b1 5125compare_symbol_name (const char *symbol_name, language symbol_language,
b5ec771e 5126 const lookup_name_info &lookup_name,
b5ec771e
PA
5127 completion_match_result &match_res)
5128{
d4c2a405 5129 const language_defn *lang = language_def (symbol_language);
1976171a 5130
b5ec771e 5131 symbol_name_matcher_ftype *name_match
618daa93 5132 = get_symbol_name_matcher (lang, lookup_name);
1976171a 5133
a207cff2 5134 return name_match (symbol_name, lookup_name, &match_res);
1976171a
JK
5135}
5136
b5ec771e 5137/* See symtab.h. */
c906108c 5138
b5ec771e 5139void
eb3ff9a5 5140completion_list_add_name (completion_tracker &tracker,
b5ec771e 5141 language symbol_language,
eb3ff9a5 5142 const char *symname,
b5ec771e 5143 const lookup_name_info &lookup_name,
0d5cff50 5144 const char *text, const char *word)
c906108c 5145{
b5ec771e
PA
5146 completion_match_result &match_res
5147 = tracker.reset_completion_match_result ();
5148
c378eb4e 5149 /* Clip symbols that cannot match. */
c62446b1 5150 if (!compare_symbol_name (symname, symbol_language, lookup_name, match_res))
1976171a 5151 return;
c906108c 5152
b5ec771e
PA
5153 /* Refresh SYMNAME from the match string. It's potentially
5154 different depending on language. (E.g., on Ada, the match may be
5155 the encoded symbol name wrapped in "<>"). */
5156 symname = match_res.match.match ();
5157 gdb_assert (symname != NULL);
5158
c906108c 5159 /* We have a match for a completion, so add SYMNAME to the current list
c378eb4e 5160 of matches. Note that the name is moved to freshly malloc'd space. */
c906108c
SS
5161
5162 {
60a20c19
PA
5163 gdb::unique_xmalloc_ptr<char> completion
5164 = make_completion_match_str (symname, text, word);
ef0b411a 5165
a207cff2
PA
5166 /* Here we pass the match-for-lcd object to add_completion. Some
5167 languages match the user text against substrings of symbol
5168 names in some cases. E.g., in C++, "b push_ba" completes to
5169 "std::vector::push_back", "std::string::push_back", etc., and
5170 in this case we want the completion lowest common denominator
5171 to be "push_back" instead of "std::". */
5172 tracker.add_completion (std::move (completion),
a22ecf70 5173 &match_res.match_for_lcd, text, word);
c906108c
SS
5174 }
5175}
5176
6da67eb1
PA
5177/* completion_list_add_name wrapper for struct symbol. */
5178
5179static void
eb3ff9a5
PA
5180completion_list_add_symbol (completion_tracker &tracker,
5181 symbol *sym,
b5ec771e 5182 const lookup_name_info &lookup_name,
6da67eb1
PA
5183 const char *text, const char *word)
5184{
b5ec771e
PA
5185 completion_list_add_name (tracker, SYMBOL_LANGUAGE (sym),
5186 SYMBOL_NATURAL_NAME (sym),
1b026119 5187 lookup_name, text, word);
6da67eb1
PA
5188}
5189
5190/* completion_list_add_name wrapper for struct minimal_symbol. */
5191
5192static void
eb3ff9a5
PA
5193completion_list_add_msymbol (completion_tracker &tracker,
5194 minimal_symbol *sym,
b5ec771e 5195 const lookup_name_info &lookup_name,
6da67eb1
PA
5196 const char *text, const char *word)
5197{
b5ec771e
PA
5198 completion_list_add_name (tracker, MSYMBOL_LANGUAGE (sym),
5199 MSYMBOL_NATURAL_NAME (sym),
1b026119 5200 lookup_name, text, word);
6da67eb1
PA
5201}
5202
b5ec771e 5203
69636828
AF
5204/* ObjC: In case we are completing on a selector, look as the msymbol
5205 again and feed all the selectors into the mill. */
5206
5207static void
eb3ff9a5
PA
5208completion_list_objc_symbol (completion_tracker &tracker,
5209 struct minimal_symbol *msymbol,
b5ec771e 5210 const lookup_name_info &lookup_name,
0d5cff50 5211 const char *text, const char *word)
69636828
AF
5212{
5213 static char *tmp = NULL;
5214 static unsigned int tmplen = 0;
9af17804 5215
0d5cff50 5216 const char *method, *category, *selector;
69636828 5217 char *tmp2 = NULL;
9af17804 5218
efd66ac6 5219 method = MSYMBOL_NATURAL_NAME (msymbol);
69636828
AF
5220
5221 /* Is it a method? */
5222 if ((method[0] != '-') && (method[0] != '+'))
5223 return;
5224
1b026119 5225 if (text[0] == '[')
69636828 5226 /* Complete on shortened method method. */
b5ec771e
PA
5227 completion_list_add_name (tracker, language_objc,
5228 method + 1,
5229 lookup_name,
1b026119 5230 text, word);
9af17804 5231
69636828
AF
5232 while ((strlen (method) + 1) >= tmplen)
5233 {
5234 if (tmplen == 0)
5235 tmplen = 1024;
5236 else
5237 tmplen *= 2;
224c3ddb 5238 tmp = (char *) xrealloc (tmp, tmplen);
69636828
AF
5239 }
5240 selector = strchr (method, ' ');
5241 if (selector != NULL)
5242 selector++;
9af17804 5243
69636828 5244 category = strchr (method, '(');
9af17804 5245
69636828
AF
5246 if ((category != NULL) && (selector != NULL))
5247 {
5248 memcpy (tmp, method, (category - method));
5249 tmp[category - method] = ' ';
5250 memcpy (tmp + (category - method) + 1, selector, strlen (selector) + 1);
b5ec771e 5251 completion_list_add_name (tracker, language_objc, tmp,
1b026119
PA
5252 lookup_name, text, word);
5253 if (text[0] == '[')
b5ec771e 5254 completion_list_add_name (tracker, language_objc, tmp + 1,
1b026119 5255 lookup_name, text, word);
69636828 5256 }
9af17804 5257
69636828
AF
5258 if (selector != NULL)
5259 {
5260 /* Complete on selector only. */
5261 strcpy (tmp, selector);
5262 tmp2 = strchr (tmp, ']');
5263 if (tmp2 != NULL)
5264 *tmp2 = '\0';
9af17804 5265
b5ec771e 5266 completion_list_add_name (tracker, language_objc, tmp,
1b026119 5267 lookup_name, text, word);
69636828
AF
5268 }
5269}
5270
5271/* Break the non-quoted text based on the characters which are in
c378eb4e 5272 symbols. FIXME: This should probably be language-specific. */
69636828 5273
6f937416
PA
5274static const char *
5275language_search_unquoted_string (const char *text, const char *p)
69636828
AF
5276{
5277 for (; p > text; --p)
5278 {
5279 if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0')
5280 continue;
5281 else
5282 {
5283 if ((current_language->la_language == language_objc))
5284 {
c378eb4e 5285 if (p[-1] == ':') /* Might be part of a method name. */
69636828
AF
5286 continue;
5287 else if (p[-1] == '[' && (p[-2] == '-' || p[-2] == '+'))
c378eb4e 5288 p -= 2; /* Beginning of a method name. */
69636828 5289 else if (p[-1] == ' ' || p[-1] == '(' || p[-1] == ')')
c378eb4e 5290 { /* Might be part of a method name. */
6f937416 5291 const char *t = p;
69636828
AF
5292
5293 /* Seeing a ' ' or a '(' is not conclusive evidence
5294 that we are in the middle of a method name. However,
5295 finding "-[" or "+[" should be pretty un-ambiguous.
5296 Unfortunately we have to find it now to decide. */
5297
5298 while (t > text)
5299 if (isalnum (t[-1]) || t[-1] == '_' ||
5300 t[-1] == ' ' || t[-1] == ':' ||
5301 t[-1] == '(' || t[-1] == ')')
5302 --t;
5303 else
5304 break;
5305
5306 if (t[-1] == '[' && (t[-2] == '-' || t[-2] == '+'))
c378eb4e
MS
5307 p = t - 2; /* Method name detected. */
5308 /* Else we leave with p unchanged. */
69636828
AF
5309 }
5310 }
5311 break;
5312 }
5313 }
5314 return p;
5315}
5316
edb3359d 5317static void
eb3ff9a5
PA
5318completion_list_add_fields (completion_tracker &tracker,
5319 struct symbol *sym,
b5ec771e 5320 const lookup_name_info &lookup_name,
eb3ff9a5 5321 const char *text, const char *word)
edb3359d
DJ
5322{
5323 if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
5324 {
5325 struct type *t = SYMBOL_TYPE (sym);
5326 enum type_code c = TYPE_CODE (t);
5327 int j;
5328
5329 if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
5330 for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++)
5331 if (TYPE_FIELD_NAME (t, j))
b5ec771e
PA
5332 completion_list_add_name (tracker, SYMBOL_LANGUAGE (sym),
5333 TYPE_FIELD_NAME (t, j),
1b026119 5334 lookup_name, text, word);
edb3359d
DJ
5335 }
5336}
5337
f9d67a22
PA
5338/* See symtab.h. */
5339
5340bool
5341symbol_is_function_or_method (symbol *sym)
5342{
5343 switch (TYPE_CODE (SYMBOL_TYPE (sym)))
5344 {
5345 case TYPE_CODE_FUNC:
5346 case TYPE_CODE_METHOD:
5347 return true;
5348 default:
5349 return false;
5350 }
5351}
5352
5353/* See symtab.h. */
5354
5355bool
5356symbol_is_function_or_method (minimal_symbol *msymbol)
5357{
5358 switch (MSYMBOL_TYPE (msymbol))
5359 {
5360 case mst_text:
5361 case mst_text_gnu_ifunc:
5362 case mst_solib_trampoline:
5363 case mst_file_text:
5364 return true;
5365 default:
5366 return false;
5367 }
5368}
5369
ca31ab1d
PA
5370/* See symtab.h. */
5371
5372bound_minimal_symbol
5373find_gnu_ifunc (const symbol *sym)
5374{
5375 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
5376 return {};
5377
5378 lookup_name_info lookup_name (SYMBOL_SEARCH_NAME (sym),
5379 symbol_name_match_type::SEARCH_NAME);
5380 struct objfile *objfile = symbol_objfile (sym);
5381
2b1ffcfd 5382 CORE_ADDR address = BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym));
ca31ab1d
PA
5383 minimal_symbol *ifunc = NULL;
5384
5385 iterate_over_minimal_symbols (objfile, lookup_name,
5386 [&] (minimal_symbol *minsym)
5387 {
5388 if (MSYMBOL_TYPE (minsym) == mst_text_gnu_ifunc
f50776aa 5389 || MSYMBOL_TYPE (minsym) == mst_data_gnu_ifunc)
ca31ab1d 5390 {
f50776aa
PA
5391 CORE_ADDR msym_addr = MSYMBOL_VALUE_ADDRESS (objfile, minsym);
5392 if (MSYMBOL_TYPE (minsym) == mst_data_gnu_ifunc)
5393 {
5394 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8b88a78e
PA
5395 msym_addr
5396 = gdbarch_convert_from_func_ptr_addr (gdbarch,
5397 msym_addr,
5398 current_top_target ());
f50776aa
PA
5399 }
5400 if (msym_addr == address)
5401 {
5402 ifunc = minsym;
5403 return true;
5404 }
ca31ab1d
PA
5405 }
5406 return false;
5407 });
5408
5409 if (ifunc != NULL)
5410 return {ifunc, objfile};
5411 return {};
5412}
5413
e11c72c7
GB
5414/* Add matching symbols from SYMTAB to the current completion list. */
5415
5416static void
5417add_symtab_completions (struct compunit_symtab *cust,
eb3ff9a5 5418 completion_tracker &tracker,
f9d67a22 5419 complete_symbol_mode mode,
b5ec771e 5420 const lookup_name_info &lookup_name,
e11c72c7
GB
5421 const char *text, const char *word,
5422 enum type_code code)
5423{
5424 struct symbol *sym;
5425 const struct block *b;
5426 struct block_iterator iter;
5427 int i;
5428
ff6fa247
GB
5429 if (cust == NULL)
5430 return;
5431
e11c72c7
GB
5432 for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
5433 {
5434 QUIT;
5435 b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), i);
5436 ALL_BLOCK_SYMBOLS (b, iter, sym)
5437 {
f9d67a22
PA
5438 if (completion_skip_symbol (mode, sym))
5439 continue;
5440
e11c72c7
GB
5441 if (code == TYPE_CODE_UNDEF
5442 || (SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN
5443 && TYPE_CODE (SYMBOL_TYPE (sym)) == code))
eb3ff9a5 5444 completion_list_add_symbol (tracker, sym,
b5ec771e 5445 lookup_name,
e11c72c7
GB
5446 text, word);
5447 }
5448 }
5449}
5450
eb3ff9a5
PA
5451void
5452default_collect_symbol_completion_matches_break_on
b5ec771e
PA
5453 (completion_tracker &tracker, complete_symbol_mode mode,
5454 symbol_name_match_type name_match_type,
eb3ff9a5
PA
5455 const char *text, const char *word,
5456 const char *break_on, enum type_code code)
c906108c 5457{
41d27058
JB
5458 /* Problem: All of the symbols have to be copied because readline
5459 frees them. I'm not going to worry about this; hopefully there
5460 won't be that many. */
5461
de4f826b 5462 struct symbol *sym;
3977b71f 5463 const struct block *b;
edb3359d 5464 const struct block *surrounding_static_block, *surrounding_global_block;
8157b174 5465 struct block_iterator iter;
c906108c 5466 /* The symbol we are completing on. Points in same buffer as text. */
6f937416 5467 const char *sym_text;
c906108c 5468
41d27058 5469 /* Now look for the symbol we are supposed to complete on. */
c6756f62
PA
5470 if (mode == complete_symbol_mode::LINESPEC)
5471 sym_text = text;
5472 else
c906108c 5473 {
6f937416 5474 const char *p;
c906108c 5475 char quote_found;
6f937416 5476 const char *quote_pos = NULL;
c906108c
SS
5477
5478 /* First see if this is a quoted string. */
5479 quote_found = '\0';
5480 for (p = text; *p != '\0'; ++p)
5481 {
5482 if (quote_found != '\0')
5483 {
5484 if (*p == quote_found)
5485 /* Found close quote. */
5486 quote_found = '\0';
5487 else if (*p == '\\' && p[1] == quote_found)
5488 /* A backslash followed by the quote character
c5aa993b 5489 doesn't end the string. */
c906108c
SS
5490 ++p;
5491 }
5492 else if (*p == '\'' || *p == '"')
5493 {
5494 quote_found = *p;
5495 quote_pos = p;
5496 }
5497 }
5498 if (quote_found == '\'')
5499 /* A string within single quotes can be a symbol, so complete on it. */
5500 sym_text = quote_pos + 1;
5501 else if (quote_found == '"')
5502 /* A double-quoted string is never a symbol, nor does it make sense
c5aa993b 5503 to complete it any other way. */
c94fdfd0 5504 {
ef0b411a 5505 return;
c94fdfd0 5506 }
c906108c
SS
5507 else
5508 {
5509 /* It is not a quoted string. Break it based on the characters
5510 which are in symbols. */
5511 while (p > text)
5512 {
95699ff0 5513 if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0'
f55ee35c 5514 || p[-1] == ':' || strchr (break_on, p[-1]) != NULL)
c906108c
SS
5515 --p;
5516 else
5517 break;
5518 }
5519 sym_text = p;
5520 }
5521 }
5522
1b026119 5523 lookup_name_info lookup_name (sym_text, name_match_type, true);
b5ec771e 5524
c906108c
SS
5525 /* At this point scan through the misc symbol vectors and add each
5526 symbol you find to the list. Eventually we want to ignore
5527 anything that isn't a text symbol (everything else will be
e11c72c7 5528 handled by the psymtab code below). */
c906108c 5529
2f68a895
TT
5530 if (code == TYPE_CODE_UNDEF)
5531 {
2030c079 5532 for (objfile *objfile : current_program_space->objfiles ())
2f68a895 5533 {
7932255d 5534 for (minimal_symbol *msymbol : objfile->msymbols ())
5325b9bf
TT
5535 {
5536 QUIT;
9af17804 5537
5325b9bf
TT
5538 if (completion_skip_symbol (mode, msymbol))
5539 continue;
f9d67a22 5540
5325b9bf
TT
5541 completion_list_add_msymbol (tracker, msymbol, lookup_name,
5542 sym_text, word);
eb3ff9a5 5543
5325b9bf
TT
5544 completion_list_objc_symbol (tracker, msymbol, lookup_name,
5545 sym_text, word);
5546 }
2f68a895
TT
5547 }
5548 }
c906108c 5549
e11c72c7 5550 /* Add completions for all currently loaded symbol tables. */
2030c079 5551 for (objfile *objfile : current_program_space->objfiles ())
d8aeb77f 5552 {
b669c953 5553 for (compunit_symtab *cust : objfile->compunits ())
d8aeb77f
TT
5554 add_symtab_completions (cust, tracker, mode, lookup_name,
5555 sym_text, word, code);
5556 }
e11c72c7 5557
14bc53a8
PA
5558 /* Look through the partial symtabs for all symbols which begin by
5559 matching SYM_TEXT. Expand all CUs that you find to the list. */
5560 expand_symtabs_matching (NULL,
b5ec771e
PA
5561 lookup_name,
5562 NULL,
14bc53a8
PA
5563 [&] (compunit_symtab *symtab) /* expansion notify */
5564 {
5565 add_symtab_completions (symtab,
f9d67a22 5566 tracker, mode, lookup_name,
1b026119 5567 sym_text, word, code);
14bc53a8
PA
5568 },
5569 ALL_DOMAIN);
e11c72c7 5570
c906108c 5571 /* Search upwards from currently selected frame (so that we can
edb3359d
DJ
5572 complete on local vars). Also catch fields of types defined in
5573 this places which match our text string. Only complete on types
c378eb4e 5574 visible from current context. */
edb3359d
DJ
5575
5576 b = get_selected_block (0);
5577 surrounding_static_block = block_static_block (b);
5578 surrounding_global_block = block_global_block (b);
5579 if (surrounding_static_block != NULL)
5580 while (b != surrounding_static_block)
5581 {
5582 QUIT;
c906108c 5583
edb3359d
DJ
5584 ALL_BLOCK_SYMBOLS (b, iter, sym)
5585 {
2f68a895
TT
5586 if (code == TYPE_CODE_UNDEF)
5587 {
b5ec771e 5588 completion_list_add_symbol (tracker, sym, lookup_name,
1b026119 5589 sym_text, word);
b5ec771e 5590 completion_list_add_fields (tracker, sym, lookup_name,
1b026119 5591 sym_text, word);
2f68a895
TT
5592 }
5593 else if (SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN
5594 && TYPE_CODE (SYMBOL_TYPE (sym)) == code)
b5ec771e 5595 completion_list_add_symbol (tracker, sym, lookup_name,
1b026119 5596 sym_text, word);
edb3359d 5597 }
c5aa993b 5598
edb3359d
DJ
5599 /* Stop when we encounter an enclosing function. Do not stop for
5600 non-inlined functions - the locals of the enclosing function
5601 are in scope for a nested function. */
5602 if (BLOCK_FUNCTION (b) != NULL && block_inlined_p (b))
5603 break;
5604 b = BLOCK_SUPERBLOCK (b);
5605 }
c906108c 5606
edb3359d 5607 /* Add fields from the file's types; symbols will be added below. */
c906108c 5608
2f68a895
TT
5609 if (code == TYPE_CODE_UNDEF)
5610 {
5611 if (surrounding_static_block != NULL)
5612 ALL_BLOCK_SYMBOLS (surrounding_static_block, iter, sym)
b5ec771e 5613 completion_list_add_fields (tracker, sym, lookup_name,
1b026119 5614 sym_text, word);
edb3359d 5615
2f68a895
TT
5616 if (surrounding_global_block != NULL)
5617 ALL_BLOCK_SYMBOLS (surrounding_global_block, iter, sym)
b5ec771e 5618 completion_list_add_fields (tracker, sym, lookup_name,
1b026119 5619 sym_text, word);
2f68a895 5620 }
c906108c 5621
2f68a895
TT
5622 /* Skip macros if we are completing a struct tag -- arguable but
5623 usually what is expected. */
5624 if (current_language->la_macro_expansion == macro_expansion_c
5625 && code == TYPE_CODE_UNDEF)
9a044a89 5626 {
f6c2623e 5627 gdb::unique_xmalloc_ptr<struct macro_scope> scope;
9a044a89 5628
14bc53a8
PA
5629 /* This adds a macro's name to the current completion list. */
5630 auto add_macro_name = [&] (const char *macro_name,
5631 const macro_definition *,
5632 macro_source_file *,
5633 int)
5634 {
1b026119
PA
5635 completion_list_add_name (tracker, language_c, macro_name,
5636 lookup_name, sym_text, word);
14bc53a8
PA
5637 };
5638
9a044a89
TT
5639 /* Add any macros visible in the default scope. Note that this
5640 may yield the occasional wrong result, because an expression
5641 might be evaluated in a scope other than the default. For
5642 example, if the user types "break file:line if <TAB>", the
5643 resulting expression will be evaluated at "file:line" -- but
5644 at there does not seem to be a way to detect this at
5645 completion time. */
5646 scope = default_macro_scope ();
5647 if (scope)
f6c2623e
TT
5648 macro_for_each_in_scope (scope->file, scope->line,
5649 add_macro_name);
9a044a89
TT
5650
5651 /* User-defined macros are always visible. */
14bc53a8 5652 macro_for_each (macro_user_macros, add_macro_name);
9a044a89 5653 }
ef0b411a
GB
5654}
5655
eb3ff9a5
PA
5656void
5657default_collect_symbol_completion_matches (completion_tracker &tracker,
c6756f62 5658 complete_symbol_mode mode,
b5ec771e 5659 symbol_name_match_type name_match_type,
eb3ff9a5
PA
5660 const char *text, const char *word,
5661 enum type_code code)
f55ee35c 5662{
c6756f62 5663 return default_collect_symbol_completion_matches_break_on (tracker, mode,
b5ec771e 5664 name_match_type,
eb3ff9a5
PA
5665 text, word, "",
5666 code);
f55ee35c
JK
5667}
5668
eb3ff9a5
PA
5669/* Collect all symbols (regardless of class) which begin by matching
5670 TEXT. */
41d27058 5671
eb3ff9a5
PA
5672void
5673collect_symbol_completion_matches (completion_tracker &tracker,
c6756f62 5674 complete_symbol_mode mode,
b5ec771e 5675 symbol_name_match_type name_match_type,
eb3ff9a5 5676 const char *text, const char *word)
41d27058 5677{
c6756f62 5678 current_language->la_collect_symbol_completion_matches (tracker, mode,
b5ec771e 5679 name_match_type,
eb3ff9a5
PA
5680 text, word,
5681 TYPE_CODE_UNDEF);
2f68a895
TT
5682}
5683
eb3ff9a5
PA
5684/* Like collect_symbol_completion_matches, but only collect
5685 STRUCT_DOMAIN symbols whose type code is CODE. */
2f68a895 5686
eb3ff9a5
PA
5687void
5688collect_symbol_completion_matches_type (completion_tracker &tracker,
5689 const char *text, const char *word,
5690 enum type_code code)
2f68a895 5691{
c6756f62 5692 complete_symbol_mode mode = complete_symbol_mode::EXPRESSION;
b5ec771e 5693 symbol_name_match_type name_match_type = symbol_name_match_type::EXPRESSION;
c6756f62 5694
2f68a895
TT
5695 gdb_assert (code == TYPE_CODE_UNION
5696 || code == TYPE_CODE_STRUCT
2f68a895 5697 || code == TYPE_CODE_ENUM);
c6756f62 5698 current_language->la_collect_symbol_completion_matches (tracker, mode,
b5ec771e 5699 name_match_type,
eb3ff9a5 5700 text, word, code);
41d27058
JB
5701}
5702
eb3ff9a5
PA
5703/* Like collect_symbol_completion_matches, but collects a list of
5704 symbols defined in all source files named SRCFILE. */
c94fdfd0 5705
eb3ff9a5
PA
5706void
5707collect_file_symbol_completion_matches (completion_tracker &tracker,
c6756f62 5708 complete_symbol_mode mode,
b5ec771e 5709 symbol_name_match_type name_match_type,
eb3ff9a5
PA
5710 const char *text, const char *word,
5711 const char *srcfile)
c94fdfd0 5712{
c94fdfd0 5713 /* The symbol we are completing on. Points in same buffer as text. */
6f937416 5714 const char *sym_text;
c94fdfd0
EZ
5715
5716 /* Now look for the symbol we are supposed to complete on.
5717 FIXME: This should be language-specific. */
c6756f62
PA
5718 if (mode == complete_symbol_mode::LINESPEC)
5719 sym_text = text;
5720 else
c94fdfd0 5721 {
6f937416 5722 const char *p;
c94fdfd0 5723 char quote_found;
6f937416 5724 const char *quote_pos = NULL;
c94fdfd0
EZ
5725
5726 /* First see if this is a quoted string. */
5727 quote_found = '\0';
5728 for (p = text; *p != '\0'; ++p)
5729 {
5730 if (quote_found != '\0')
5731 {
5732 if (*p == quote_found)
5733 /* Found close quote. */
5734 quote_found = '\0';
5735 else if (*p == '\\' && p[1] == quote_found)
5736 /* A backslash followed by the quote character
5737 doesn't end the string. */
5738 ++p;
5739 }
5740 else if (*p == '\'' || *p == '"')
5741 {
5742 quote_found = *p;
5743 quote_pos = p;
5744 }
5745 }
5746 if (quote_found == '\'')
5747 /* A string within single quotes can be a symbol, so complete on it. */
5748 sym_text = quote_pos + 1;
5749 else if (quote_found == '"')
5750 /* A double-quoted string is never a symbol, nor does it make sense
5751 to complete it any other way. */
5752 {
eb3ff9a5 5753 return;
c94fdfd0
EZ
5754 }
5755 else
5756 {
69636828
AF
5757 /* Not a quoted string. */
5758 sym_text = language_search_unquoted_string (text, p);
c94fdfd0
EZ
5759 }
5760 }
5761
1b026119 5762 lookup_name_info lookup_name (sym_text, name_match_type, true);
b5ec771e 5763
8f14146e
PA
5764 /* Go through symtabs for SRCFILE and check the externs and statics
5765 for symbols which match. */
5766 iterate_over_symtabs (srcfile, [&] (symtab *s)
c94fdfd0 5767 {
8f14146e 5768 add_symtab_completions (SYMTAB_COMPUNIT (s),
f9d67a22 5769 tracker, mode, lookup_name,
1b026119 5770 sym_text, word, TYPE_CODE_UNDEF);
8f14146e
PA
5771 return false;
5772 });
e27852be
DE
5773}
5774
c94fdfd0
EZ
5775/* A helper function for make_source_files_completion_list. It adds
5776 another file name to a list of possible completions, growing the
5777 list as necessary. */
5778
5779static void
6f937416 5780add_filename_to_list (const char *fname, const char *text, const char *word,
eb3ff9a5 5781 completion_list *list)
c94fdfd0 5782{
60a20c19 5783 list->emplace_back (make_completion_match_str (fname, text, word));
c94fdfd0
EZ
5784}
5785
5786static int
5787not_interesting_fname (const char *fname)
5788{
5789 static const char *illegal_aliens[] = {
5790 "_globals_", /* inserted by coff_symtab_read */
5791 NULL
5792 };
5793 int i;
5794
5795 for (i = 0; illegal_aliens[i]; i++)
5796 {
0ba1096a 5797 if (filename_cmp (fname, illegal_aliens[i]) == 0)
c94fdfd0
EZ
5798 return 1;
5799 }
5800 return 0;
5801}
5802
ccefe4c4
TT
5803/* An object of this type is passed as the user_data argument to
5804 map_partial_symbol_filenames. */
5805struct add_partial_filename_data
5806{
9fdc877b 5807 struct filename_seen_cache *filename_seen_cache;
6f937416
PA
5808 const char *text;
5809 const char *word;
ccefe4c4 5810 int text_len;
eb3ff9a5 5811 completion_list *list;
ccefe4c4
TT
5812};
5813
5814/* A callback for map_partial_symbol_filenames. */
eca864fe 5815
ccefe4c4 5816static void
2837d59e 5817maybe_add_partial_symtab_filename (const char *filename, const char *fullname,
ccefe4c4
TT
5818 void *user_data)
5819{
19ba03f4
SM
5820 struct add_partial_filename_data *data
5821 = (struct add_partial_filename_data *) user_data;
ccefe4c4
TT
5822
5823 if (not_interesting_fname (filename))
5824 return;
bbf2f4df 5825 if (!data->filename_seen_cache->seen (filename)
0ba1096a 5826 && filename_ncmp (filename, data->text, data->text_len) == 0)
ccefe4c4
TT
5827 {
5828 /* This file matches for a completion; add it to the
5829 current list of matches. */
49c4e619 5830 add_filename_to_list (filename, data->text, data->word, data->list);
ccefe4c4
TT
5831 }
5832 else
5833 {
5834 const char *base_name = lbasename (filename);
433759f7 5835
ccefe4c4 5836 if (base_name != filename
bbf2f4df 5837 && !data->filename_seen_cache->seen (base_name)
0ba1096a 5838 && filename_ncmp (base_name, data->text, data->text_len) == 0)
49c4e619 5839 add_filename_to_list (base_name, data->text, data->word, data->list);
ccefe4c4
TT
5840 }
5841}
5842
eb3ff9a5 5843/* Return a list of all source files whose names begin with matching
49c4e619 5844 TEXT. The file names are looked up in the symbol tables of this
eb3ff9a5 5845 program. */
c94fdfd0 5846
eb3ff9a5 5847completion_list
6f937416 5848make_source_files_completion_list (const char *text, const char *word)
c94fdfd0 5849{
c94fdfd0 5850 size_t text_len = strlen (text);
eb3ff9a5 5851 completion_list list;
31889e00 5852 const char *base_name;
ccefe4c4 5853 struct add_partial_filename_data datum;
c94fdfd0 5854
c94fdfd0
EZ
5855 if (!have_full_symbols () && !have_partial_symbols ())
5856 return list;
5857
bbf2f4df 5858 filename_seen_cache filenames_seen;
9fdc877b 5859
2030c079 5860 for (objfile *objfile : current_program_space->objfiles ())
c94fdfd0 5861 {
b669c953 5862 for (compunit_symtab *cu : objfile->compunits ())
c94fdfd0 5863 {
8b31193a
TT
5864 for (symtab *s : compunit_filetabs (cu))
5865 {
5866 if (not_interesting_fname (s->filename))
5867 continue;
5868 if (!filenames_seen.seen (s->filename)
5869 && filename_ncmp (s->filename, text, text_len) == 0)
5870 {
5871 /* This file matches for a completion; add it to the current
5872 list of matches. */
5873 add_filename_to_list (s->filename, text, word, &list);
5874 }
5875 else
5876 {
5877 /* NOTE: We allow the user to type a base name when the
5878 debug info records leading directories, but not the other
5879 way around. This is what subroutines of breakpoint
5880 command do when they parse file names. */
5881 base_name = lbasename (s->filename);
5882 if (base_name != s->filename
5883 && !filenames_seen.seen (base_name)
5884 && filename_ncmp (base_name, text, text_len) == 0)
5885 add_filename_to_list (base_name, text, word, &list);
5886 }
5887 }
c94fdfd0
EZ
5888 }
5889 }
5890
bbf2f4df 5891 datum.filename_seen_cache = &filenames_seen;
ccefe4c4
TT
5892 datum.text = text;
5893 datum.word = word;
5894 datum.text_len = text_len;
5895 datum.list = &list;
bb4142cf
DE
5896 map_symbol_filenames (maybe_add_partial_symtab_filename, &datum,
5897 0 /*need_fullname*/);
9fdc877b 5898
c94fdfd0
EZ
5899 return list;
5900}
c906108c 5901\f
51cc5b07 5902/* Track MAIN */
32ac0d11
TT
5903
5904/* Return the "main_info" object for the current program space. If
5905 the object has not yet been created, create it and fill in some
5906 default values. */
5907
5908static struct main_info *
5909get_main_info (void)
5910{
a32ad8c5 5911 struct main_info *info = main_progspace_key.get (current_program_space);
32ac0d11
TT
5912
5913 if (info == NULL)
5914 {
3d548a53
TT
5915 /* It may seem strange to store the main name in the progspace
5916 and also in whatever objfile happens to see a main name in
5917 its debug info. The reason for this is mainly historical:
5918 gdb returned "main" as the name even if no function named
5919 "main" was defined the program; and this approach lets us
5920 keep compatibility. */
a32ad8c5 5921 info = main_progspace_key.emplace (current_program_space);
32ac0d11
TT
5922 }
5923
5924 return info;
5925}
5926
3d548a53 5927static void
9e6c82ad 5928set_main_name (const char *name, enum language lang)
51cc5b07 5929{
32ac0d11
TT
5930 struct main_info *info = get_main_info ();
5931
5932 if (info->name_of_main != NULL)
51cc5b07 5933 {
32ac0d11
TT
5934 xfree (info->name_of_main);
5935 info->name_of_main = NULL;
5936 info->language_of_main = language_unknown;
51cc5b07
AC
5937 }
5938 if (name != NULL)
5939 {
32ac0d11
TT
5940 info->name_of_main = xstrdup (name);
5941 info->language_of_main = lang;
51cc5b07
AC
5942 }
5943}
5944
ea53e89f
JB
5945/* Deduce the name of the main procedure, and set NAME_OF_MAIN
5946 accordingly. */
5947
5948static void
5949find_main_name (void)
5950{
cd6c7346 5951 const char *new_main_name;
3d548a53
TT
5952
5953 /* First check the objfiles to see whether a debuginfo reader has
5954 picked up the appropriate main name. Historically the main name
5955 was found in a more or less random way; this approach instead
5956 relies on the order of objfile creation -- which still isn't
5957 guaranteed to get the correct answer, but is just probably more
5958 accurate. */
2030c079 5959 for (objfile *objfile : current_program_space->objfiles ())
aed57c53
TT
5960 {
5961 if (objfile->per_bfd->name_of_main != NULL)
5962 {
5963 set_main_name (objfile->per_bfd->name_of_main,
5964 objfile->per_bfd->language_of_main);
5965 return;
5966 }
5967 }
ea53e89f
JB
5968
5969 /* Try to see if the main procedure is in Ada. */
5970 /* FIXME: brobecker/2005-03-07: Another way of doing this would
5971 be to add a new method in the language vector, and call this
5972 method for each language until one of them returns a non-empty
5973 name. This would allow us to remove this hard-coded call to
5974 an Ada function. It is not clear that this is a better approach
5975 at this point, because all methods need to be written in a way
c378eb4e 5976 such that false positives never be returned. For instance, it is
ea53e89f
JB
5977 important that a method does not return a wrong name for the main
5978 procedure if the main procedure is actually written in a different
5979 language. It is easy to guaranty this with Ada, since we use a
5980 special symbol generated only when the main in Ada to find the name
c378eb4e 5981 of the main procedure. It is difficult however to see how this can
ea53e89f
JB
5982 be guarantied for languages such as C, for instance. This suggests
5983 that order of call for these methods becomes important, which means
5984 a more complicated approach. */
5985 new_main_name = ada_main_name ();
5986 if (new_main_name != NULL)
9af17804 5987 {
9e6c82ad 5988 set_main_name (new_main_name, language_ada);
ea53e89f
JB
5989 return;
5990 }
5991
63778547
IB
5992 new_main_name = d_main_name ();
5993 if (new_main_name != NULL)
5994 {
5995 set_main_name (new_main_name, language_d);
5996 return;
5997 }
5998
a766d390
DE
5999 new_main_name = go_main_name ();
6000 if (new_main_name != NULL)
6001 {
9e6c82ad 6002 set_main_name (new_main_name, language_go);
a766d390
DE
6003 return;
6004 }
6005
cd6c7346
PM
6006 new_main_name = pascal_main_name ();
6007 if (new_main_name != NULL)
9af17804 6008 {
9e6c82ad 6009 set_main_name (new_main_name, language_pascal);
cd6c7346
PM
6010 return;
6011 }
6012
ea53e89f
JB
6013 /* The languages above didn't identify the name of the main procedure.
6014 Fallback to "main". */
9e6c82ad 6015 set_main_name ("main", language_unknown);
ea53e89f
JB
6016}
6017
cd215b2e
TT
6018/* See symtab.h. */
6019
6020const char *
6021main_name ()
51cc5b07 6022{
32ac0d11
TT
6023 struct main_info *info = get_main_info ();
6024
6025 if (info->name_of_main == NULL)
ea53e89f
JB
6026 find_main_name ();
6027
32ac0d11 6028 return info->name_of_main;
51cc5b07
AC
6029}
6030
9e6c82ad
TT
6031/* Return the language of the main function. If it is not known,
6032 return language_unknown. */
6033
6034enum language
6035main_language (void)
6036{
32ac0d11
TT
6037 struct main_info *info = get_main_info ();
6038
6039 if (info->name_of_main == NULL)
6040 find_main_name ();
6041
6042 return info->language_of_main;
9e6c82ad
TT
6043}
6044
ea53e89f
JB
6045/* Handle ``executable_changed'' events for the symtab module. */
6046
6047static void
781b42b0 6048symtab_observer_executable_changed (void)
ea53e89f
JB
6049{
6050 /* NAME_OF_MAIN may no longer be the same, so reset it for now. */
9e6c82ad 6051 set_main_name (NULL, language_unknown);
ea53e89f 6052}
51cc5b07 6053
a6c727b2
DJ
6054/* Return 1 if the supplied producer string matches the ARM RealView
6055 compiler (armcc). */
6056
ececd218 6057bool
a6c727b2
DJ
6058producer_is_realview (const char *producer)
6059{
6060 static const char *const arm_idents[] = {
6061 "ARM C Compiler, ADS",
6062 "Thumb C Compiler, ADS",
6063 "ARM C++ Compiler, ADS",
6064 "Thumb C++ Compiler, ADS",
6065 "ARM/Thumb C/C++ Compiler, RVCT",
6066 "ARM C/C++ Compiler, RVCT"
6067 };
6068 int i;
6069
6070 if (producer == NULL)
ececd218 6071 return false;
a6c727b2
DJ
6072
6073 for (i = 0; i < ARRAY_SIZE (arm_idents); i++)
61012eef 6074 if (startswith (producer, arm_idents[i]))
ececd218 6075 return true;
a6c727b2 6076
ececd218 6077 return false;
a6c727b2 6078}
ed0616c6 6079
f1e6e072
TT
6080\f
6081
6082/* The next index to hand out in response to a registration request. */
6083
6084static int next_aclass_value = LOC_FINAL_VALUE;
6085
6086/* The maximum number of "aclass" registrations we support. This is
6087 constant for convenience. */
6088#define MAX_SYMBOL_IMPLS (LOC_FINAL_VALUE + 10)
6089
6090/* The objects representing the various "aclass" values. The elements
6091 from 0 up to LOC_FINAL_VALUE-1 represent themselves, and subsequent
6092 elements are those registered at gdb initialization time. */
6093
6094static struct symbol_impl symbol_impl[MAX_SYMBOL_IMPLS];
6095
6096/* The globally visible pointer. This is separate from 'symbol_impl'
6097 so that it can be const. */
6098
6099const struct symbol_impl *symbol_impls = &symbol_impl[0];
6100
6101/* Make sure we saved enough room in struct symbol. */
6102
6103gdb_static_assert (MAX_SYMBOL_IMPLS <= (1 << SYMBOL_ACLASS_BITS));
6104
6105/* Register a computed symbol type. ACLASS must be LOC_COMPUTED. OPS
6106 is the ops vector associated with this index. This returns the new
6107 index, which should be used as the aclass_index field for symbols
6108 of this type. */
6109
6110int
6111register_symbol_computed_impl (enum address_class aclass,
6112 const struct symbol_computed_ops *ops)
6113{
6114 int result = next_aclass_value++;
6115
6116 gdb_assert (aclass == LOC_COMPUTED);
6117 gdb_assert (result < MAX_SYMBOL_IMPLS);
6118 symbol_impl[result].aclass = aclass;
6119 symbol_impl[result].ops_computed = ops;
6120
24d6c2a0
TT
6121 /* Sanity check OPS. */
6122 gdb_assert (ops != NULL);
6123 gdb_assert (ops->tracepoint_var_ref != NULL);
6124 gdb_assert (ops->describe_location != NULL);
0b31a4bc 6125 gdb_assert (ops->get_symbol_read_needs != NULL);
24d6c2a0
TT
6126 gdb_assert (ops->read_variable != NULL);
6127
f1e6e072
TT
6128 return result;
6129}
6130
6131/* Register a function with frame base type. ACLASS must be LOC_BLOCK.
6132 OPS is the ops vector associated with this index. This returns the
6133 new index, which should be used as the aclass_index field for symbols
6134 of this type. */
6135
6136int
6137register_symbol_block_impl (enum address_class aclass,
6138 const struct symbol_block_ops *ops)
6139{
6140 int result = next_aclass_value++;
6141
6142 gdb_assert (aclass == LOC_BLOCK);
6143 gdb_assert (result < MAX_SYMBOL_IMPLS);
6144 symbol_impl[result].aclass = aclass;
6145 symbol_impl[result].ops_block = ops;
6146
6147 /* Sanity check OPS. */
6148 gdb_assert (ops != NULL);
6149 gdb_assert (ops->find_frame_base_location != NULL);
6150
6151 return result;
6152}
6153
6154/* Register a register symbol type. ACLASS must be LOC_REGISTER or
6155 LOC_REGPARM_ADDR. OPS is the register ops vector associated with
6156 this index. This returns the new index, which should be used as
6157 the aclass_index field for symbols of this type. */
6158
6159int
6160register_symbol_register_impl (enum address_class aclass,
6161 const struct symbol_register_ops *ops)
6162{
6163 int result = next_aclass_value++;
6164
6165 gdb_assert (aclass == LOC_REGISTER || aclass == LOC_REGPARM_ADDR);
6166 gdb_assert (result < MAX_SYMBOL_IMPLS);
6167 symbol_impl[result].aclass = aclass;
6168 symbol_impl[result].ops_register = ops;
6169
6170 return result;
6171}
6172
6173/* Initialize elements of 'symbol_impl' for the constants in enum
6174 address_class. */
6175
6176static void
6177initialize_ordinary_address_classes (void)
6178{
6179 int i;
6180
6181 for (i = 0; i < LOC_FINAL_VALUE; ++i)
aead7601 6182 symbol_impl[i].aclass = (enum address_class) i;
f1e6e072
TT
6183}
6184
6185\f
6186
1994afbf
DE
6187/* Helper function to initialize the fields of an objfile-owned symbol.
6188 It assumed that *SYM is already all zeroes. */
6189
6190static void
6191initialize_objfile_symbol_1 (struct symbol *sym)
6192{
6193 SYMBOL_OBJFILE_OWNED (sym) = 1;
6194 SYMBOL_SECTION (sym) = -1;
6195}
6196
6197/* Initialize the symbol SYM, and mark it as being owned by an objfile. */
e623cf5d
TT
6198
6199void
38bf1463 6200initialize_objfile_symbol (struct symbol *sym)
e623cf5d
TT
6201{
6202 memset (sym, 0, sizeof (*sym));
1994afbf 6203 initialize_objfile_symbol_1 (sym);
e623cf5d
TT
6204}
6205
6206/* Allocate and initialize a new 'struct symbol' on OBJFILE's
6207 obstack. */
6208
6209struct symbol *
6210allocate_symbol (struct objfile *objfile)
6211{
6212 struct symbol *result;
6213
6214 result = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
1994afbf 6215 initialize_objfile_symbol_1 (result);
e623cf5d
TT
6216
6217 return result;
6218}
6219
6220/* Allocate and initialize a new 'struct template_symbol' on OBJFILE's
6221 obstack. */
6222
6223struct template_symbol *
6224allocate_template_symbol (struct objfile *objfile)
6225{
6226 struct template_symbol *result;
6227
6228 result = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct template_symbol);
68e745e3 6229 initialize_objfile_symbol_1 (result);
e623cf5d
TT
6230
6231 return result;
6232}
6233
08be3fe3
DE
6234/* See symtab.h. */
6235
6236struct objfile *
6237symbol_objfile (const struct symbol *symbol)
6238{
1994afbf
DE
6239 gdb_assert (SYMBOL_OBJFILE_OWNED (symbol));
6240 return SYMTAB_OBJFILE (symbol->owner.symtab);
08be3fe3
DE
6241}
6242
6243/* See symtab.h. */
6244
6245struct gdbarch *
6246symbol_arch (const struct symbol *symbol)
6247{
1994afbf
DE
6248 if (!SYMBOL_OBJFILE_OWNED (symbol))
6249 return symbol->owner.arch;
6250 return get_objfile_arch (SYMTAB_OBJFILE (symbol->owner.symtab));
08be3fe3
DE
6251}
6252
6253/* See symtab.h. */
6254
6255struct symtab *
6256symbol_symtab (const struct symbol *symbol)
6257{
1994afbf
DE
6258 gdb_assert (SYMBOL_OBJFILE_OWNED (symbol));
6259 return symbol->owner.symtab;
08be3fe3
DE
6260}
6261
6262/* See symtab.h. */
6263
6264void
6265symbol_set_symtab (struct symbol *symbol, struct symtab *symtab)
6266{
1994afbf
DE
6267 gdb_assert (SYMBOL_OBJFILE_OWNED (symbol));
6268 symbol->owner.symtab = symtab;
08be3fe3
DE
6269}
6270
4b610737
TT
6271/* See symtab.h. */
6272
6273CORE_ADDR
6274get_symbol_address (const struct symbol *sym)
6275{
6276 gdb_assert (sym->maybe_copied);
6277 gdb_assert (SYMBOL_CLASS (sym) == LOC_STATIC);
6278
6279 const char *linkage_name = SYMBOL_LINKAGE_NAME (sym);
6280
6281 for (objfile *objfile : current_program_space->objfiles ())
6282 {
6283 bound_minimal_symbol minsym
6284 = lookup_minimal_symbol_linkage (linkage_name, objfile);
6285 if (minsym.minsym != nullptr)
6286 return BMSYMBOL_VALUE_ADDRESS (minsym);
6287 }
6288 return sym->ginfo.value.address;
6289}
6290
6291/* See symtab.h. */
6292
6293CORE_ADDR
6294get_msymbol_address (struct objfile *objf, const struct minimal_symbol *minsym)
6295{
6296 gdb_assert (minsym->maybe_copied);
6297 gdb_assert ((objf->flags & OBJF_MAINLINE) == 0);
6298
6299 const char *linkage_name = MSYMBOL_LINKAGE_NAME (minsym);
6300
6301 for (objfile *objfile : current_program_space->objfiles ())
6302 {
6303 if ((objfile->flags & OBJF_MAINLINE) != 0)
6304 {
6305 bound_minimal_symbol found
6306 = lookup_minimal_symbol_linkage (linkage_name, objfile);
6307 if (found.minsym != nullptr)
6308 return BMSYMBOL_VALUE_ADDRESS (found);
6309 }
6310 }
6311 return (minsym->value.address
6312 + ANOFFSET (objf->section_offsets, minsym->section));
6313}
6314
e623cf5d
TT
6315\f
6316
c906108c 6317void
fba45db2 6318_initialize_symtab (void)
c906108c 6319{
60cfcb20
AB
6320 cmd_list_element *c;
6321
f1e6e072
TT
6322 initialize_ordinary_address_classes ();
6323
60cfcb20
AB
6324 c = add_info ("variables", info_variables_command,
6325 info_print_args_help (_("\
12615cba 6326All global and static variable names or those matching REGEXPs.\n\
4acfdd20 6327Usage: info variables [-q] [-n] [-t TYPEREGEXP] [NAMEREGEXP]\n\
12615cba 6328Prints the global and static variables.\n"),
4acfdd20
AB
6329 _("global and static variables"),
6330 true));
60cfcb20 6331 set_cmd_completer_handle_brkchars (c, info_print_command_completer);
c906108c 6332 if (dbx_commands)
60cfcb20
AB
6333 {
6334 c = add_com ("whereis", class_info, info_variables_command,
6335 info_print_args_help (_("\
12615cba 6336All global and static variable names, or those matching REGEXPs.\n\
4acfdd20 6337Usage: whereis [-q] [-n] [-t TYPEREGEXP] [NAMEREGEXP]\n\
12615cba 6338Prints the global and static variables.\n"),
4acfdd20
AB
6339 _("global and static variables"),
6340 true));
60cfcb20
AB
6341 set_cmd_completer_handle_brkchars (c, info_print_command_completer);
6342 }
c906108c 6343
60cfcb20
AB
6344 c = add_info ("functions", info_functions_command,
6345 info_print_args_help (_("\
12615cba 6346All function names or those matching REGEXPs.\n\
4acfdd20 6347Usage: info functions [-q] [-n] [-t TYPEREGEXP] [NAMEREGEXP]\n\
12615cba 6348Prints the functions.\n"),
4acfdd20
AB
6349 _("functions"),
6350 true));
60cfcb20 6351 set_cmd_completer_handle_brkchars (c, info_print_command_completer);
c906108c 6352
a8eab7c6
AB
6353 c = add_info ("types", info_types_command, _("\
6354All type names, or those matching REGEXP.\n\
6355Usage: info types [-q] [REGEXP]\n\
6356Print information about all types matching REGEXP, or all types if no\n\
6357REGEXP is given. The optional flag -q disables printing of headers."));
6358 set_cmd_completer_handle_brkchars (c, info_types_command_completer);
c906108c 6359
28cd9371
PW
6360 const auto info_sources_opts = make_info_sources_options_def_group (nullptr);
6361
6362 static std::string info_sources_help
6363 = gdb::option::build_help (_("\
6364All source files in the program or those matching REGEXP.\n\
6365Usage: info sources [OPTION]... [REGEXP]\n\
6366By default, REGEXP is used to match anywhere in the filename.\n\
6367\n\
6368Options:\n\
6369%OPTIONS%"),
6370 info_sources_opts);
6371
6372 c = add_info ("sources", info_sources_command, info_sources_help.c_str ());
6373 set_cmd_completer_handle_brkchars (c, info_sources_command_completer);
c906108c
SS
6374
6375 add_com ("rbreak", class_breakpoint, rbreak_command,
1bedd215 6376 _("Set a breakpoint for all functions matching REGEXP."));
c906108c 6377
717d2f5a
JB
6378 add_setshow_enum_cmd ("multiple-symbols", no_class,
6379 multiple_symbols_modes, &multiple_symbols_mode,
6380 _("\
590042fc 6381Set how the debugger handles ambiguities in expressions."), _("\
717d2f5a
JB
6382Show how the debugger handles ambiguities in expressions."), _("\
6383Valid values are \"ask\", \"all\", \"cancel\", and the default is \"all\"."),
6384 NULL, NULL, &setlist, &showlist);
6385
c011a4f4
DE
6386 add_setshow_boolean_cmd ("basenames-may-differ", class_obscure,
6387 &basenames_may_differ, _("\
6388Set whether a source file may have multiple base names."), _("\
6389Show whether a source file may have multiple base names."), _("\
6390(A \"base name\" is the name of a file with the directory part removed.\n\
6391Example: The base name of \"/home/user/hello.c\" is \"hello.c\".)\n\
6392If set, GDB will canonicalize file names (e.g., expand symlinks)\n\
6393before comparing them. Canonicalization is an expensive operation,\n\
6394but it allows the same file be known by more than one base name.\n\
6395If not set (the default), all source files are assumed to have just\n\
6396one base name, and gdb will do file name comparisons more efficiently."),
6397 NULL, NULL,
6398 &setlist, &showlist);
6399
db0fec5c
DE
6400 add_setshow_zuinteger_cmd ("symtab-create", no_class, &symtab_create_debug,
6401 _("Set debugging of symbol table creation."),
6402 _("Show debugging of symbol table creation."), _("\
6403When enabled (non-zero), debugging messages are printed when building\n\
6404symbol tables. A value of 1 (one) normally provides enough information.\n\
6405A value greater than 1 provides more verbose information."),
6406 NULL,
6407 NULL,
6408 &setdebuglist, &showdebuglist);
45cfd468 6409
cc485e62
DE
6410 add_setshow_zuinteger_cmd ("symbol-lookup", no_class, &symbol_lookup_debug,
6411 _("\
6412Set debugging of symbol lookup."), _("\
6413Show debugging of symbol lookup."), _("\
6414When enabled (non-zero), symbol lookups are logged."),
6415 NULL, NULL,
6416 &setdebuglist, &showdebuglist);
6417
f57d2163
DE
6418 add_setshow_zuinteger_cmd ("symbol-cache-size", no_class,
6419 &new_symbol_cache_size,
6420 _("Set the size of the symbol cache."),
6421 _("Show the size of the symbol cache."), _("\
6422The size of the symbol cache.\n\
6423If zero then the symbol cache is disabled."),
6424 set_symbol_cache_size_handler, NULL,
6425 &maintenance_set_cmdlist,
6426 &maintenance_show_cmdlist);
6427
6428 add_cmd ("symbol-cache", class_maintenance, maintenance_print_symbol_cache,
6429 _("Dump the symbol cache for each program space."),
6430 &maintenanceprintlist);
6431
6432 add_cmd ("symbol-cache-statistics", class_maintenance,
6433 maintenance_print_symbol_cache_statistics,
6434 _("Print symbol cache statistics for each program space."),
6435 &maintenanceprintlist);
6436
6437 add_cmd ("flush-symbol-cache", class_maintenance,
6438 maintenance_flush_symbol_cache,
6439 _("Flush the symbol cache for each program space."),
6440 &maintenancelist);
6441
76727919
TT
6442 gdb::observers::executable_changed.attach (symtab_observer_executable_changed);
6443 gdb::observers::new_objfile.attach (symtab_new_objfile_observer);
6444 gdb::observers::free_objfile.attach (symtab_free_objfile_observer);
c906108c 6445}
This page took 2.753724 seconds and 4 git commands to generate.