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