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