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