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