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