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