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