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