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