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