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