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