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