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