]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame_incremental - gdb/symtab.c
gas: introduce .errif and .warnif
[thirdparty/binutils-gdb.git] / gdb / symtab.c
... / ...
CommitLineData
1/* Symbol table lookup for the GNU debugger, GDB.
2
3 Copyright (C) 1986-2025 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "dwarf2/call-site.h"
21#include "exceptions.h"
22#include "symtab.h"
23#include "event-top.h"
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"
31#include "gdbsupport/gdb_regex.h"
32#include "expression.h"
33#include "language.h"
34#include "demangle.h"
35#include "inferior.h"
36#include "source.h"
37#include "filenames.h"
38#include "objc-lang.h"
39#include "d-lang.h"
40#include "ada-lang.h"
41#include "go-lang.h"
42#include "p-lang.h"
43#include "addrmap.h"
44#include "cli/cli-utils.h"
45#include "cli/cli-style.h"
46#include "cli/cli-cmds.h"
47#include "fnmatch.h"
48#include "hashtab.h"
49#include "typeprint.h"
50#include "exceptions.h"
51
52#include "gdbsupport/gdb_obstack.h"
53#include "block.h"
54#include "dictionary.h"
55
56#include <sys/types.h>
57#include <fcntl.h>
58#include <sys/stat.h>
59#include <ctype.h>
60#include "cp-abi.h"
61#include "cp-support.h"
62#include "observable.h"
63#include "macrotab.h"
64#include "macroscope.h"
65
66#include "parser-defs.h"
67#include "completer.h"
68#include "progspace-and-thread.h"
69#include <optional>
70#include "filename-seen-cache.h"
71#include "arch-utils.h"
72#include <algorithm>
73#include <string_view>
74#include "gdbsupport/pathstuff.h"
75#include "gdbsupport/common-utils.h"
76#include <optional>
77#include "gdbsupport/unordered_set.h"
78
79/* Forward declarations for local functions. */
80
81static void rbreak_command (const char *, int);
82
83static int find_line_common (const linetable *, int, int *, int);
84
85static struct block_symbol
86 lookup_symbol_aux (const char *name,
87 symbol_name_match_type match_type,
88 const struct block *block,
89 const domain_search_flags domain,
90 enum language language,
91 struct field_of_this_result *);
92
93static
94struct block_symbol lookup_local_symbol (const char *name,
95 symbol_name_match_type match_type,
96 const struct block *block,
97 const domain_search_flags domain,
98 const struct language_defn *langdef);
99
100static struct block_symbol
101 lookup_symbol_in_objfile (struct objfile *objfile,
102 enum block_enum block_index,
103 const char *name,
104 const domain_search_flags domain);
105
106static void set_main_name (program_space *pspace, const char *name,
107 language lang);
108
109/* Type of the data stored on the program space. */
110
111struct main_info
112{
113 /* Name of "main". */
114
115 std::string name_of_main;
116
117 /* Language of "main". */
118
119 enum language language_of_main = language_unknown;
120};
121
122/* Program space key for finding name and language of "main". */
123
124static const registry<program_space>::key<main_info> main_progspace_key;
125
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
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. */
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)
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
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
211 /* The domain that was searched for initially. This must exactly
212 match. */
213 domain_search_flags domain;
214
215 union
216 {
217 struct block_symbol found;
218 char *name;
219 } value;
220};
221
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)
228 xfree (slot->value.name);
229 slot->state = SYMBOL_SLOT_UNUSED;
230}
231
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
247 struct symbol_cache_slot symbols[1];
248};
249
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
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{
275 symbol_cache () = default;
276
277 ~symbol_cache ()
278 {
279 destroy_block_symbol_cache (global_symbols);
280 destroy_block_symbol_cache (static_symbols);
281 }
282
283 struct block_symbol_cache *global_symbols = nullptr;
284 struct block_symbol_cache *static_symbols = nullptr;
285};
286
287/* Program space key for finding its symbol cache. */
288
289static const registry<program_space>::key<symbol_cache> symbol_cache_key;
290
291/* When non-zero, print debugging messages related to symtab creation. */
292unsigned int symtab_create_debug = 0;
293
294/* When non-zero, print debugging messages related to symbol lookup. */
295unsigned int symbol_lookup_debug = 0;
296
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
305/* True if a file may be known by two different basenames.
306 This is the uncommon case, and significantly slows down gdb.
307 Default set to "off" to not slow down the common case. */
308bool basenames_may_differ = false;
309
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
314const char multiple_symbols_ask[] = "ask";
315const char multiple_symbols_all[] = "all";
316const char multiple_symbols_cancel[] = "cancel";
317static const char *const multiple_symbols_modes[] =
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
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
330/* Read-only accessor to AUTO_SELECT_MODE. */
331
332const char *
333multiple_symbols_select_mode (void)
334{
335 return multiple_symbols_mode;
336}
337
338/* Return the name of a domain_enum. */
339
340const char *
341domain_name (domain_enum e)
342{
343 switch (e)
344 {
345#define SYM_DOMAIN(X) \
346 case X ## _DOMAIN: return #X "_DOMAIN";
347#include "sym-domains.def"
348#undef SYM_DOMAIN
349 default: gdb_assert_not_reached ("bad domain_enum");
350 }
351}
352
353/* See symtab.h. */
354
355std::string
356domain_name (domain_search_flags flags)
357{
358 static constexpr domain_search_flags::string_mapping mapping[] = {
359#define SYM_DOMAIN(X) \
360 MAP_ENUM_FLAG (SEARCH_ ## X ## _DOMAIN),
361#include "sym-domains.def"
362#undef SYM_DOMAIN
363 };
364
365 return flags.to_string (mapping);
366}
367
368/* See symtab.h. */
369
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 {
379#define SYM_DOMAIN(X) \
380 case X ## _DOMAIN: break;
381#include "sym-domains.def"
382#undef SYM_DOMAIN
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;
400#define SYM_DOMAIN(X) \
401 check &= ~ (int) SEARCH_ ## X ## _DOMAIN;
402#include "sym-domains.def"
403#undef SYM_DOMAIN
404 if (check != 0)
405 error (_("unrecognized domain constant"));
406 return (domain_search_flag) val;
407 }
408}
409
410/* See symtab.h. */
411
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
425CORE_ADDR
426linetable_entry::pc (const struct objfile *objfile) const
427{
428 return CORE_ADDR (m_pc) + objfile->text_section_offset ();
429}
430
431/* See symtab.h. */
432
433call_site *
434compunit_symtab::find_call_site (CORE_ADDR pc) const
435{
436 if (m_call_site_htab == nullptr)
437 return nullptr;
438
439 CORE_ADDR delta = this->objfile ()->text_section_offset ();
440
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;
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);
450
451 if (pc == new_pc)
452 return nullptr;
453
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;
457
458 return nullptr;
459}
460
461/* See symtab.h. */
462
463void
464compunit_symtab::set_call_site_htab (call_site_htab_t &&call_site_htab)
465{
466 gdb_assert (m_call_site_htab == nullptr);
467 m_call_site_htab = new call_site_htab_t (std::move (call_site_htab));
468}
469
470/* See symtab.h. */
471
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. */
478 for (symtab *filetab : this->filetabs ())
479 {
480 if (filetab == primary_filetab)
481 {
482 if (prev_filetab != nullptr)
483 {
484 prev_filetab->next = primary_filetab->next;
485 primary_filetab->next = m_filetabs;
486 m_filetabs = primary_filetab;
487 }
488
489 break;
490 }
491
492 prev_filetab = filetab;
493 }
494
495 gdb_assert (primary_filetab == m_filetabs);
496}
497
498/* See symtab.h. */
499
500struct symtab *
501compunit_symtab::primary_filetab () const
502{
503 gdb_assert (m_filetabs != nullptr);
504
505 /* The primary file symtab is the first one in the list. */
506 return m_filetabs;
507}
508
509/* See symtab.h. */
510
511enum language
512compunit_symtab::language () const
513{
514 struct symtab *symtab = primary_filetab ();
515
516 /* The language of the compunit symtab is the language of its
517 primary source file. */
518 return symtab->language ();
519}
520
521/* See symtab.h. */
522
523void
524compunit_symtab::forget_cached_source_info ()
525{
526 for (symtab *s : filetabs ())
527 s->release_fullname ();
528}
529
530/* See symtab.h. */
531
532void
533compunit_symtab::finalize ()
534{
535 this->forget_cached_source_info ();
536 delete m_call_site_htab;
537}
538
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{
545 if (this->maybe_copied (objfile))
546 return this->get_maybe_copied_address (objfile);
547 else
548 return (CORE_ADDR (this->unrelocated_address ())
549 + objfile->section_offsets[this->section_index ()]);
550}
551
552/* See symtab.h. */
553
554bool
555minimal_symbol::data_p () const
556{
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;
562}
563
564/* See symtab.h. */
565
566bool
567minimal_symbol::text_p () const
568{
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;
575}
576
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
587/* See whether FILENAME matches SEARCH_NAME using the rule that we
588 advertise to the user. (The manual's description of linespecs
589 describes what we advertise). Returns true if they match, false
590 otherwise. */
591
592bool
593compare_filenames_for_search (const char *filename, const char *search_name)
594{
595 int len = strlen (filename);
596 size_t search_len = strlen (search_name);
597
598 if (len < search_len)
599 return false;
600
601 /* The tail of FILENAME must match. */
602 if (FILENAME_CMP (filename + len - search_len, search_name) != 0)
603 return false;
604
605 /* Either the names must completely match, or the character
606 preceding the trailing SEARCH_NAME segment of FILENAME must be a
607 directory separator.
608
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
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. */
618 return (len == search_len
619 || (!IS_ABSOLUTE_PATH (search_name)
620 && IS_DIR_SEPARATOR (filename[len - search_len - 1]))
621 || (HAS_DRIVE_SPEC (filename)
622 && STRIP_DRIVE_SPEC (filename) == &filename[len - search_len]));
623}
624
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.
627
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
631 The return value, NAME, REAL_PATH and CALLBACK are identical to the
632 `map_symtabs_matching_filename' method of quick_symbol_functions.
633
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
637 search until the end of the list. */
638
639bool
640iterate_over_some_symtabs (const char *name,
641 const char *real_path,
642 struct compunit_symtab *first,
643 struct compunit_symtab *after_last,
644 gdb::function_view<bool (symtab *)> callback)
645{
646 struct compunit_symtab *cust;
647 const char* base_name = lbasename (name);
648
649 for (cust = first; cust != NULL && cust != after_last; cust = cust->next)
650 {
651 /* Skip included compunits. */
652 if (cust->user != nullptr)
653 continue;
654
655 for (symtab *s : cust->filetabs ())
656 {
657 if (compare_filenames_for_search (s->filename, name))
658 {
659 if (callback (s))
660 return true;
661 continue;
662 }
663
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;
669
670 if (compare_filenames_for_search (symtab_to_fullname (s), name))
671 {
672 if (callback (s))
673 return true;
674 continue;
675 }
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));
685 gdb::unique_xmalloc_ptr<char> fullname_real_path
686 = gdb_realpath (fullname);
687 fullname = fullname_real_path.get ();
688 if (FILENAME_CMP (real_path, fullname) == 0)
689 {
690 if (callback (s))
691 return true;
692 continue;
693 }
694 }
695 }
696 }
697
698 return false;
699}
700
701/* See symtab.h. */
702
703void
704iterate_over_symtabs (program_space *pspace, const char *name,
705 gdb::function_view<bool (symtab *)> callback)
706{
707 gdb::unique_xmalloc_ptr<char> real_path;
708
709 /* Here we are interested in canonicalizing an absolute path, not
710 absolutizing a relative path. */
711 if (IS_ABSOLUTE_PATH (name))
712 {
713 real_path = gdb_realpath (name);
714 gdb_assert (IS_ABSOLUTE_PATH (real_path.get ()));
715 }
716
717 for (objfile *objfile : pspace->objfiles ())
718 if (iterate_over_some_symtabs (name, real_path.get (),
719 objfile->compunit_symtabs, nullptr,
720 callback))
721 return;
722
723 /* Same search rules as above apply here, but now we look through the
724 psymtabs. */
725 for (objfile *objfile : pspace->objfiles ())
726 if (objfile->map_symtabs_matching_filename (name, real_path.get (),
727 callback))
728 return;
729}
730
731/* See symtab.h. */
732
733symtab *
734lookup_symtab (program_space *pspace, const char *name)
735{
736 struct symtab *result = NULL;
737
738 iterate_over_symtabs (pspace, name, [&] (symtab *symtab)
739 {
740 result = symtab;
741 return true;
742 });
743
744 return result;
745}
746
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,
751 specified by SIGNATURE_ID. Note that this function is g++ specific. */
752
753char *
754gdb_mangle_name (struct type *type, int method_id, int signature_id)
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];
760 const char *field_name = TYPE_FN_FIELDLIST_NAME (type, method_id);
761 const char *physname = TYPE_FN_FIELD_PHYSNAME (f, signature_id);
762 const char *newname = type->name ();
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;
769 int is_destructor = is_destructor_name (physname);
770 /* Need a new type prefix. */
771 const char *const_prefix = method->is_const ? "C" : "";
772 const char *volatile_prefix = method->is_volatile ? "V" : "";
773 char buf[20];
774 int len = (newname == NULL ? 0 : strlen (newname));
775
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))
780 return xstrdup (physname);
781
782 is_full_physname_constructor = is_constructor_name (physname);
783
784 is_constructor = is_full_physname_constructor
785 || (newname && strcmp (field_name, newname) == 0);
786
787 if (!is_destructor)
788 is_destructor = (startswith (physname, "__dt"));
789
790 if (is_destructor || is_full_physname_constructor)
791 {
792 mangled_name = (char *) xmalloc (strlen (physname) + 1);
793 strcpy (mangled_name, physname);
794 return mangled_name;
795 }
796
797 if (len == 0)
798 {
799 xsnprintf (buf, sizeof (buf), "__%s%s", const_prefix, volatile_prefix);
800 }
801 else if (physname[0] == 't' || physname[0] == 'Q')
802 {
803 /* The physname for template and qualified methods already includes
804 the class name. */
805 xsnprintf (buf, sizeof (buf), "__%s%s", const_prefix, volatile_prefix);
806 newname = NULL;
807 len = 0;
808 }
809 else
810 {
811 xsnprintf (buf, sizeof (buf), "__%s%s%d", const_prefix,
812 volatile_prefix, len);
813 }
814 mangled_name_len = ((is_constructor ? 0 : strlen (field_name))
815 + strlen (buf) + len + strlen (physname) + 1);
816
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
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
826 as something starting with `::' rather than `classname::'. */
827 if (newname != NULL)
828 strcat (mangled_name, newname);
829
830 strcat (mangled_name, physname);
831 return (mangled_name);
832}
833
834/* See symtab.h. */
835
836void
837general_symbol_info::set_demangled_name (const char *name,
838 struct obstack *obstack)
839{
840 if (language () == language_ada)
841 {
842 if (name == NULL)
843 {
844 ada_mangled = 0;
845 language_specific.obstack = obstack;
846 }
847 else
848 {
849 ada_mangled = 1;
850 language_specific.demangled_name = name;
851 }
852 }
853 else
854 language_specific.demangled_name = name;
855}
856
857\f
858/* Initialize the language dependent portion of a symbol
859 depending upon the language for the symbol. */
860
861void
862general_symbol_info::set_language (enum language language,
863 struct obstack *obstack)
864{
865 m_language = language;
866 if (language == language_cplus
867 || language == language_d
868 || language == language_go
869 || language == language_objc
870 || language == language_fortran)
871 {
872 set_demangled_name (NULL, obstack);
873 }
874 else if (language == language_ada)
875 {
876 gdb_assert (ada_mangled == 0);
877 language_specific.obstack = obstack;
878 }
879 else
880 {
881 memset (&language_specific, 0, sizeof (language_specific));
882 }
883}
884
885/* Functions to initialize a symbol's mangled name. */
886
887/* Objects of this type are stored in the demangled name hash table. */
888struct demangled_name_entry
889{
890 demangled_name_entry (std::string_view mangled_name)
891 : mangled (mangled_name) {}
892
893 std::string_view mangled;
894 enum language language;
895 gdb::unique_xmalloc_ptr<char> demangled;
896};
897
898/* Hash function for the demangled name hash. */
899
900static hashval_t
901hash_demangled_name_entry (const void *data)
902{
903 const struct demangled_name_entry *e
904 = (const struct demangled_name_entry *) data;
905
906 return gdb::string_view_hash () (e->mangled);
907}
908
909/* Equality function for the demangled name hash. */
910
911static int
912eq_demangled_name_entry (const void *a, const void *b)
913{
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;
918
919 return da->mangled == db->mangled;
920}
921
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
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
936create_demangled_names_hash (struct objfile_per_bfd_storage *per_bfd)
937{
938 /* Choose 256 as the starting size of the hash table, somewhat arbitrarily.
939 The hash table code will round this up to the next prime number.
940 Choosing a much larger table size wastes memory, and saves only about
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);
952
953 per_bfd->demangled_names_hash.reset (htab_create_alloc
954 (count, hash_demangled_name_entry, eq_demangled_name_entry,
955 free_demangled_name_entry, xcalloc, xfree));
956}
957
958/* See symtab.h */
959
960gdb::unique_xmalloc_ptr<char>
961symbol_find_demangled_name (struct general_symbol_info *gsymbol,
962 const char *mangled)
963{
964 gdb::unique_xmalloc_ptr<char> demangled;
965 int i;
966
967 if (gsymbol->language () != language_unknown)
968 {
969 const struct language_defn *lang = language_def (gsymbol->language ());
970
971 lang->sniff_from_mangled_name (mangled, &demangled);
972 return demangled;
973 }
974
975 for (i = language_unknown; i < nr_languages; ++i)
976 {
977 enum language l = (enum language) i;
978 const struct language_defn *lang = language_def (l);
979
980 if (lang->sniff_from_mangled_name (mangled, &demangled))
981 {
982 gsymbol->m_language = l;
983 return demangled;
984 }
985 }
986
987 return NULL;
988}
989
990/* Set both the mangled and demangled (if any) names for GSYMBOL based
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
998 comes from the per-BFD storage_obstack. LINKAGE_NAME is copied,
999 so the pointer can be discarded after calling this function. */
1000
1001void
1002general_symbol_info::compute_and_set_names (std::string_view linkage_name,
1003 bool copy_name,
1004 objfile_per_bfd_storage *per_bfd,
1005 std::optional<hashval_t> hash)
1006{
1007 struct demangled_name_entry **slot;
1008
1009 if (language () == language_ada)
1010 {
1011 /* In Ada, we do the symbol lookups using the mangled name, so
1012 we can save some space by not storing the demangled name. */
1013 if (!copy_name)
1014 m_name = linkage_name.data ();
1015 else
1016 m_name = obstack_strndup (&per_bfd->storage_obstack,
1017 linkage_name.data (),
1018 linkage_name.length ());
1019 set_demangled_name (NULL, &per_bfd->storage_obstack);
1020
1021 return;
1022 }
1023
1024 if (per_bfd->demangled_names_hash == NULL)
1025 create_demangled_names_hash (per_bfd);
1026
1027 struct demangled_name_entry entry (linkage_name);
1028 if (!hash.has_value ())
1029 hash = hash_demangled_name_entry (&entry);
1030 slot = ((struct demangled_name_entry **)
1031 htab_find_slot_with_hash (per_bfd->demangled_names_hash.get (),
1032 &entry, *hash, INSERT));
1033
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
1044 /* If this name is not in the hash table, add it. */
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. */
1048 || (language () == language_go && (*slot)->demangled == nullptr))
1049 {
1050 /* A 0-terminated copy of the linkage name. Callers must set COPY_NAME
1051 to true if the string might not be nullterminated. We have to make
1052 this copy because demangling needs a nullterminated string. */
1053 std::string_view linkage_name_copy;
1054 if (copy_name)
1055 {
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';
1059
1060 linkage_name_copy = std::string_view (alloc_name,
1061 linkage_name.length ());
1062 }
1063 else
1064 linkage_name_copy = linkage_name;
1065
1066 if (demangled_name.get () == nullptr)
1067 demangled_name
1068 = symbol_find_demangled_name (this, linkage_name_copy.data ());
1069
1070 /* Suppose we have demangled_name==NULL, copy_name==0, and
1071 linkage_name_copy==linkage_name. In this case, we already have the
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.
1075
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
1078 us better bcache hit rates for partial symbols. */
1079 if (!copy_name)
1080 {
1081 *slot
1082 = ((struct demangled_name_entry *)
1083 obstack_alloc (&per_bfd->storage_obstack,
1084 sizeof (demangled_name_entry)));
1085 new (*slot) demangled_name_entry (linkage_name);
1086 }
1087 else
1088 {
1089 /* If we must copy the mangled name, put it directly after
1090 the struct so we can have a single allocation. */
1091 *slot
1092 = ((struct demangled_name_entry *)
1093 obstack_alloc (&per_bfd->storage_obstack,
1094 sizeof (demangled_name_entry)
1095 + linkage_name.length () + 1));
1096 char *mangled_ptr = reinterpret_cast<char *> (*slot + 1);
1097 memcpy (mangled_ptr, linkage_name.data (), linkage_name.length ());
1098 mangled_ptr [linkage_name.length ()] = '\0';
1099 new (*slot) demangled_name_entry
1100 (std::string_view (mangled_ptr, linkage_name.length ()));
1101 }
1102 (*slot)->demangled = std::move (demangled_name);
1103 (*slot)->language = language ();
1104 }
1105 else if (language () == language_unknown)
1106 m_language = (*slot)->language;
1107
1108 m_name = (*slot)->mangled.data ();
1109 set_demangled_name ((*slot)->demangled.get (), &per_bfd->storage_obstack);
1110}
1111
1112/* See symtab.h. */
1113
1114const char *
1115general_symbol_info::natural_name () const
1116{
1117 switch (language ())
1118 {
1119 case language_cplus:
1120 case language_d:
1121 case language_go:
1122 case language_objc:
1123 case language_fortran:
1124 case language_rust:
1125 if (language_specific.demangled_name != nullptr)
1126 return language_specific.demangled_name;
1127 break;
1128 case language_ada:
1129 return ada_decode_symbol (this);
1130 default:
1131 break;
1132 }
1133 return linkage_name ();
1134}
1135
1136/* See symtab.h. */
1137
1138const char *
1139general_symbol_info::demangled_name () const
1140{
1141 const char *dem_name = NULL;
1142
1143 switch (language ())
1144 {
1145 case language_cplus:
1146 case language_d:
1147 case language_go:
1148 case language_objc:
1149 case language_fortran:
1150 case language_rust:
1151 dem_name = language_specific.demangled_name;
1152 break;
1153 case language_ada:
1154 dem_name = ada_decode_symbol (this);
1155 break;
1156 default:
1157 break;
1158 }
1159 return dem_name;
1160}
1161
1162/* See symtab.h. */
1163
1164const char *
1165general_symbol_info::search_name () const
1166{
1167 if (language () == language_ada)
1168 return linkage_name ();
1169 else
1170 return natural_name ();
1171}
1172
1173/* See symtab.h. */
1174
1175struct obj_section *
1176general_symbol_info::obj_section (const struct objfile *objfile) const
1177{
1178 if (section_index () >= 0)
1179 return &objfile->sections_start[section_index ()];
1180 return nullptr;
1181}
1182
1183/* See symtab.h. */
1184
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
1190 = language_def (gsymbol->language ())->get_symbol_name_matcher (name);
1191 return name_match (gsymbol->search_name (), name, NULL);
1192}
1193
1194\f
1195
1196/* Return true if the two sections are the same, or if they could
1197 plausibly be copies of each other, one in an original object
1198 file and another in a separated debug file. */
1199
1200bool
1201matching_obj_sections (struct obj_section *obj_first,
1202 struct obj_section *obj_second)
1203{
1204 asection *first = obj_first? obj_first->the_bfd_section : NULL;
1205 asection *second = obj_second? obj_second->the_bfd_section : NULL;
1206
1207 /* If they're the same section, then they match. */
1208 if (first == second)
1209 return true;
1210
1211 /* If either is NULL, give up. */
1212 if (first == NULL || second == NULL)
1213 return false;
1214
1215 /* This doesn't apply to absolute symbols. */
1216 if (first->owner == NULL || second->owner == NULL)
1217 return false;
1218
1219 /* If they're in the same object file, they must be different sections. */
1220 if (first->owner == second->owner)
1221 return false;
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. */
1227 if (bfd_section_size (first) != bfd_section_size (second))
1228 return false;
1229
1230 /* In-memory addresses may start at a different offset, relativize them. */
1231 if (bfd_section_vma (first) - bfd_get_start_address (first->owner)
1232 != bfd_section_vma (second) - bfd_get_start_address (second->owner))
1233 return false;
1234
1235 if (bfd_section_name (first) == NULL
1236 || bfd_section_name (second) == NULL
1237 || strcmp (bfd_section_name (first), bfd_section_name (second)) != 0)
1238 return false;
1239
1240 /* Otherwise check that they are in corresponding objfiles. */
1241
1242 struct objfile *obj = NULL;
1243 for (objfile *objfile : current_program_space->objfiles ())
1244 if (objfile->obfd == first->owner)
1245 {
1246 obj = objfile;
1247 break;
1248 }
1249 gdb_assert (obj != NULL);
1250
1251 if (obj->separate_debug_objfile != NULL
1252 && obj->separate_debug_objfile->obfd == second->owner)
1253 return true;
1254 if (obj->separate_debug_objfile_backlink != NULL
1255 && obj->separate_debug_objfile_backlink->obfd == second->owner)
1256 return true;
1257
1258 return false;
1259}
1260\f
1261/* Hash function for the symbol cache. */
1262
1263static unsigned int
1264hash_symbol_entry (const struct objfile *objfile_context,
1265 const char *name, domain_search_flags domain)
1266{
1267 unsigned int hash = (uintptr_t) objfile_context;
1268
1269 if (name != NULL)
1270 hash += htab_hash_string (name);
1271
1272 hash += domain * 7;
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,
1282 const char *name, domain_search_flags domain)
1283{
1284 const char *slot_name;
1285
1286 if (slot->state == SYMBOL_SLOT_UNUSED)
1287 return 0;
1288
1289 if (slot->objfile_context != objfile_context)
1290 return 0;
1291
1292 domain_search_flags slot_domain = slot->domain;
1293 if (slot->state == SYMBOL_SLOT_NOT_FOUND)
1294 slot_name = slot->value.name;
1295 else
1296 slot_name = slot->value.found.symbol->search_name ();
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 {
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
1311 the symbol's language with symbol->search_name (). See
1312 dictionary.c.
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
1317 if (slot_domain != domain)
1318 return 0;
1319
1320 if (slot->state == SYMBOL_SLOT_NOT_FOUND)
1321 {
1322 if (strcmp (slot_name, name) != 0)
1323 return 0;
1324 }
1325 else
1326 {
1327 struct symbol *sym = slot->value.found.symbol;
1328 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
1329
1330 if (!symbol_matches_search_name (sym, lookup_name))
1331 return 0;
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
1367 destroy_block_symbol_cache (cache->global_symbols);
1368 destroy_block_symbol_cache (cache->static_symbols);
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
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);
1383 cache->global_symbols->size = new_size;
1384 cache->static_symbols->size = new_size;
1385 }
1386}
1387
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{
1394 struct symbol_cache *cache = symbol_cache_key.get (pspace);
1395
1396 if (cache == NULL)
1397 {
1398 cache = symbol_cache_key.emplace (pspace);
1399 resize_symbol_cache (cache, symbol_cache_size);
1400 }
1401
1402 return cache;
1403}
1404
1405/* Set the size of the symbol cache in all program spaces. */
1406
1407static void
1408set_symbol_cache_size (unsigned int new_size)
1409{
1410 for (struct program_space *pspace : program_spaces)
1411 {
1412 struct symbol_cache *cache = symbol_cache_key.get (pspace);
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
1423set_symbol_cache_size_handler (const char *args, int from_tty,
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.
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. */
1447
1448static struct block_symbol
1449symbol_cache_lookup (struct symbol_cache *cache,
1450 struct objfile *objfile_context, enum block_enum block,
1451 const char *name, domain_search_flags domain,
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;
1467 return {};
1468 }
1469
1470 hash = hash_symbol_entry (objfile_context, name, domain);
1471 slot = bsc->symbols + hash % bsc->size;
1472
1473 *bsc_ptr = bsc;
1474 *slot_ptr = slot;
1475
1476 if (eq_symbol_entry (slot, objfile_context, name, domain))
1477 {
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,
1482 domain_name (domain).c_str ());
1483 ++bsc->hits;
1484 if (slot->state == SYMBOL_SLOT_NOT_FOUND)
1485 return SYMBOL_LOOKUP_FAILED;
1486 return slot->value.found;
1487 }
1488
1489 /* Symbol is not present in the cache. */
1490
1491 symbol_lookup_debug_printf ("%s block symbol cache miss for %s, %s",
1492 block == GLOBAL_BLOCK ? "Global" : "Static",
1493 name, domain_name (domain).c_str ());
1494 ++bsc->misses;
1495 return {};
1496}
1497
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,
1507 struct symbol *symbol,
1508 const struct block *block,
1509 domain_search_flags domain)
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;
1520 slot->value.found.symbol = symbol;
1521 slot->value.found.block = block;
1522 slot->domain = domain;
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,
1533 const char *name, domain_search_flags domain)
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;
1544 slot->value.name = xstrdup (name);
1545 slot->domain = domain;
1546}
1547
1548/* Flush the symbol cache of PSPACE. */
1549
1550static void
1551symbol_cache_flush (struct program_space *pspace)
1552{
1553 ada_clear_symbol_cache (pspace);
1554 struct symbol_cache *cache = symbol_cache_key.get (pspace);
1555 int pass;
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 {
1603 gdb_printf (" <disabled>\n");
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)
1614 gdb_printf ("Global symbols:\n");
1615 else
1616 gdb_printf ("Static symbols:\n");
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:
1629 gdb_printf (" [%4u] = %s, %s %s (not found)\n", i,
1630 host_address_to_string (slot->objfile_context),
1631 slot->value.name,
1632 domain_name (slot->domain).c_str ());
1633 break;
1634 case SYMBOL_SLOT_FOUND:
1635 {
1636 struct symbol *found = slot->value.found.symbol;
1637 const struct objfile *context = slot->objfile_context;
1638
1639 gdb_printf (" [%4u] = %s, %s %s\n", i,
1640 host_address_to_string (context),
1641 found->print_name (),
1642 domain_name (found->domain ()));
1643 break;
1644 }
1645 }
1646 }
1647 }
1648}
1649
1650/* The "mt print symbol-cache" command. */
1651
1652static void
1653maintenance_print_symbol_cache (const char *args, int from_tty)
1654{
1655 for (struct program_space *pspace : program_spaces)
1656 {
1657 struct symbol_cache *cache;
1658
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)");
1664
1665 /* If the cache hasn't been created yet, avoid creating one. */
1666 cache = symbol_cache_key.get (pspace);
1667 if (cache == NULL)
1668 gdb_printf (" <empty>\n");
1669 else
1670 symbol_cache_dump (cache);
1671 }
1672}
1673
1674/* The "mt flush-symbol-cache" command. */
1675
1676static void
1677maintenance_flush_symbol_cache (const char *args, int from_tty)
1678{
1679 for (struct program_space *pspace : program_spaces)
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 {
1694 gdb_printf (" <disabled>\n");
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)
1706 gdb_printf ("Global block cache stats:\n");
1707 else
1708 gdb_printf ("Static block cache stats:\n");
1709
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);
1714 }
1715}
1716
1717/* The "mt print symbol-cache-statistics" command. */
1718
1719static void
1720maintenance_print_symbol_cache_statistics (const char *args, int from_tty)
1721{
1722 for (struct program_space *pspace : program_spaces)
1723 {
1724 struct symbol_cache *cache;
1725
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)");
1731
1732 /* If the cache hasn't been created yet, avoid creating one. */
1733 cache = symbol_cache_key.get (pspace);
1734 if (cache == NULL)
1735 gdb_printf (" empty, no stats available\n");
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{
1746 symbol_cache_flush (objfile->pspace ());
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);
1755
1756 /* Forget everything we know about the main function. */
1757 main_progspace_key.clear (pspace);
1758}
1759
1760/* This module's 'free_objfile' observer. */
1761
1762static void
1763symtab_free_objfile_observer (struct objfile *objfile)
1764{
1765 symbol_cache_flush (objfile->pspace ());
1766}
1767\f
1768/* See symtab.h. */
1769
1770void
1771fixup_symbol_section (struct symbol *sym, struct objfile *objfile)
1772{
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
1801 struct minimal_symbol *msym;
1802
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. */
1808 msym = lookup_minimal_symbol_by_pc_name (addr, sym->linkage_name (),
1809 objfile);
1810 if (msym)
1811 sym->set_section_index (msym->section_index ());
1812 else
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.
1823
1824 So, instead, search the section table when lookup by name has
1825 failed. The ``addr'' and ``endaddr'' fields may have already
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.
1830
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
1842 symbol, only its section.
1843
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. */
1848
1849 for (obj_section *s : objfile->sections ())
1850 {
1851 if ((bfd_section_flags (s->the_bfd_section) & SEC_ALLOC) == 0)
1852 continue;
1853
1854 int idx = s - objfile->sections_start;
1855 CORE_ADDR offset = objfile->section_offsets[idx];
1856
1857 if (fallback == -1)
1858 fallback = idx;
1859
1860 if (s->addr () - offset <= addr && addr < s->endaddr () - offset)
1861 {
1862 sym->set_section_index (idx);
1863 return;
1864 }
1865 }
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)
1871 sym->set_section_index (0);
1872 else
1873 sym->set_section_index (fallback);
1874 }
1875}
1876
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
1884 if (lookup_name.ignore_parameters () && lang == language_cplus)
1885 {
1886 gdb::unique_xmalloc_ptr<char> without_params
1887 = cp_remove_params_if_any (lookup_name.c_str (),
1888 lookup_name.completion_mode ());
1889
1890 if (without_params != NULL)
1891 {
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);
1895 return;
1896 }
1897 }
1898
1899 if (lookup_name.match_type () == symbol_name_match_type::SEARCH_NAME)
1900 m_demangled_name = lookup_name.c_str ();
1901 else
1902 m_demangled_name = demangle_for_lookup (lookup_name.c_str (),
1903 lang, storage);
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. */
1913 static const lookup_name_info lookup_name ("", symbol_name_match_type::WILD,
1914 true);
1915
1916 return lookup_name;
1917}
1918
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
1941/* Compute the demangled form of NAME as used by the various symbol
1942 lookup functions. The result can either be the input NAME
1943 directly, or a pointer to a buffer owned by the STORAGE object.
1944
1945 For Ada, this function just returns NAME, unmodified.
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. */
1958
1959const char *
1960demangle_for_lookup (const char *name, enum language lang,
1961 demangle_result_storage &storage)
1962{
1963 /* If we are using C++, D, or Go, demangle the name before doing a
1964 lookup, so we can always binary search. */
1965 if (lang == language_cplus)
1966 {
1967 gdb::unique_xmalloc_ptr<char> demangled_name
1968 = gdb_demangle (name, DMGL_ANSI | DMGL_PARAMS);
1969 if (demangled_name != NULL)
1970 return storage.set_malloc_ptr (std::move (demangled_name));
1971
1972 /* If we were given a non-mangled name, canonicalize it
1973 according to the language (so far only for C++). */
1974 gdb::unique_xmalloc_ptr<char> canon = cp_canonicalize_string (name);
1975 if (canon != nullptr)
1976 return storage.set_malloc_ptr (std::move (canon));
1977 }
1978 else if (lang == language_d)
1979 {
1980 gdb::unique_xmalloc_ptr<char> demangled_name = d_demangle (name, 0);
1981 if (demangled_name != NULL)
1982 return storage.set_malloc_ptr (std::move (demangled_name));
1983 }
1984 else if (lang == language_go)
1985 {
1986 gdb::unique_xmalloc_ptr<char> demangled_name
1987 = language_def (language_go)->demangle_symbol (name, 0);
1988 if (demangled_name != NULL)
1989 return storage.set_malloc_ptr (std::move (demangled_name));
1990 }
1991
1992 return name;
1993}
1994
1995/* See symtab.h. */
1996
1997unsigned int
1998search_name_hash (enum language language, const char *search_name)
1999{
2000 return language_def (language)->search_name_hash (search_name);
2001}
2002
2003/* See symtab.h.
2004
2005 This function (or rather its subordinates) have a bunch of loops and
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
2008 are a few calls for which it would appear to be bad news to quit
2009 out of here: e.g., find_proc_desc in alpha-mdebug-tdep.c. (Note
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
2015struct block_symbol
2016lookup_symbol_in_language (const char *name, const struct block *block,
2017 const domain_search_flags domain,
2018 enum language lang,
2019 struct field_of_this_result *is_a_field_of_this)
2020{
2021 SYMBOL_LOOKUP_SCOPED_DEBUG_ENTER_EXIT;
2022
2023 demangle_result_storage storage;
2024 const char *modified_name = demangle_for_lookup (name, lang, storage);
2025
2026 return lookup_symbol_aux (modified_name,
2027 symbol_name_match_type::FULL,
2028 block, domain, lang,
2029 is_a_field_of_this);
2030}
2031
2032/* See symtab.h. */
2033
2034struct block_symbol
2035lookup_symbol (const char *name, const struct block *block,
2036 domain_search_flags domain,
2037 struct field_of_this_result *is_a_field_of_this)
2038{
2039 return lookup_symbol_in_language (name, block, domain,
2040 current_language->la_language,
2041 is_a_field_of_this);
2042}
2043
2044/* See symtab.h. */
2045
2046struct block_symbol
2047lookup_symbol_search_name (const char *search_name, const struct block *block,
2048 domain_search_flags domain)
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
2056struct block_symbol
2057lookup_language_this (const struct language_defn *lang,
2058 const struct block *block)
2059{
2060 if (lang->name_of_this () == NULL || block == NULL)
2061 return {};
2062
2063 symbol_lookup_debug_printf_v ("lookup_language_this (%s, %s (objfile %s))",
2064 lang->name (), host_address_to_string (block),
2065 objfile_debug_name (block->objfile ()));
2066
2067 lookup_name_info this_name (lang->name_of_this (),
2068 symbol_name_match_type::SEARCH_NAME);
2069
2070 while (block)
2071 {
2072 struct symbol *sym;
2073
2074 sym = block_lookup_symbol (block, this_name, SEARCH_VFT);
2075 if (sym != NULL)
2076 {
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));
2081 return (struct block_symbol) {sym, block};
2082 }
2083 if (block->function ())
2084 break;
2085 block = block->superblock ();
2086 }
2087
2088 symbol_lookup_debug_printf_v ("lookup_language_this (...) = NULL");
2089 return {};
2090}
2091
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
2097check_field (struct type *type, const char *name,
2098 struct field_of_this_result *is_a_field_of_this)
2099{
2100 int i;
2101
2102 /* The type may be a stub. */
2103 type = check_typedef (type);
2104
2105 for (i = type->num_fields () - 1; i >= TYPE_N_BASECLASSES (type); i--)
2106 {
2107 const char *t_field_name = type->field (i).name ();
2108
2109 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
2110 {
2111 is_a_field_of_this->type = type;
2112 is_a_field_of_this->field = &type->field (i);
2113 return 1;
2114 }
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)
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 }
2128 }
2129
2130 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
2131 if (check_field (TYPE_BASECLASS (type, i), name, is_a_field_of_this))
2132 return 1;
2133
2134 return 0;
2135}
2136
2137/* Behave like lookup_symbol except that NAME is the natural name
2138 (e.g., demangled name) of the symbol that we're looking for. */
2139
2140static struct block_symbol
2141lookup_symbol_aux (const char *name, symbol_name_match_type match_type,
2142 const struct block *block,
2143 const domain_search_flags domain, enum language language,
2144 struct field_of_this_result *is_a_field_of_this)
2145{
2146 SYMBOL_LOOKUP_SCOPED_DEBUG_ENTER_EXIT;
2147
2148 struct block_symbol result;
2149 const struct language_defn *langdef;
2150
2151 if (symbol_lookup_debug)
2152 {
2153 struct objfile *objfile = (block == nullptr
2154 ? nullptr : block->objfile ());
2155
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\")",
2162 domain_name (domain).c_str (), language_str (language));
2163 }
2164
2165 langdef = language_def (language);
2166
2167 /* Search specified block and its superiors. Don't search
2168 STATIC_BLOCK or GLOBAL_BLOCK. */
2169
2170 result = lookup_local_symbol (name, match_type, block, domain, langdef);
2171 if (result.symbol != NULL)
2172 {
2173 symbol_lookup_debug_printf
2174 ("found symbol @ %s (using lookup_local_symbol)",
2175 host_address_to_string (result.symbol));
2176 return result;
2177 }
2178
2179 /* If requested to do so by the caller and if appropriate for LANGUAGE,
2180 check to see if NAME is a field of `this'. */
2181
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. */
2185 if (is_a_field_of_this != NULL && (domain & SEARCH_STRUCT_DOMAIN) == 0)
2186 {
2187 result = lookup_language_this (langdef, block);
2188
2189 if (result.symbol)
2190 {
2191 struct type *t = result.symbol->type ();
2192
2193 /* I'm not really sure that type of this can ever
2194 be typedefed; just be safe. */
2195 t = check_typedef (t);
2196 if (t->is_pointer_or_reference ())
2197 t = t->target_type ();
2198
2199 if (t->code () != TYPE_CODE_STRUCT
2200 && t->code () != TYPE_CODE_UNION)
2201 error (_("Internal error: `%s' is not an aggregate"),
2202 langdef->name_of_this ());
2203
2204 if (check_field (t, name, is_a_field_of_this))
2205 {
2206 symbol_lookup_debug_printf ("no symbol found");
2207 return {};
2208 }
2209 }
2210 }
2211
2212 /* Now do whatever is appropriate for LANGUAGE to look
2213 up static and global variables. */
2214
2215 result = langdef->lookup_symbol_nonlocal (name, block, domain);
2216 if (result.symbol != NULL)
2217 {
2218 symbol_lookup_debug_printf
2219 ("found symbol @ %s (using language lookup_symbol_nonlocal)",
2220 host_address_to_string (result.symbol));
2221 return result;
2222 }
2223
2224 /* Now search all static file-level symbols. Not strictly correct,
2225 but more useful than an error. */
2226
2227 result = lookup_static_symbol (name, domain);
2228 symbol_lookup_debug_printf
2229 ("found symbol @ %s (using lookup_static_symbol)",
2230 result.symbol != NULL ? host_address_to_string (result.symbol) : "NULL");
2231 return result;
2232}
2233
2234/* Check to see if the symbol is defined in BLOCK or its superiors.
2235 Don't search STATIC_BLOCK or GLOBAL_BLOCK. */
2236
2237static struct block_symbol
2238lookup_local_symbol (const char *name,
2239 symbol_name_match_type match_type,
2240 const struct block *block,
2241 const domain_search_flags domain,
2242 const struct language_defn *langdef)
2243{
2244 if (block == nullptr)
2245 return {};
2246
2247 const char *scope = block->scope ();
2248
2249 while (!block->is_global_block () && !block->is_static_block ())
2250 {
2251 struct symbol *sym = lookup_symbol_in_block (name, match_type,
2252 block, domain);
2253 if (sym != NULL)
2254 return (struct block_symbol) {sym, block};
2255
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
2267 struct block_symbol blocksym
2268 = langdef->lookup_symbol_local (scope, name, block, domain);
2269 if (blocksym.symbol != nullptr)
2270 return blocksym;
2271
2272 if (block->inlined_p ())
2273 break;
2274 block = block->superblock ();
2275 }
2276
2277 /* We've reached the end of the function without finding a result. */
2278
2279 return {};
2280}
2281
2282/* See symtab.h. */
2283
2284struct symbol *
2285lookup_symbol_in_block (const char *name, symbol_name_match_type match_type,
2286 const struct block *block,
2287 const domain_search_flags domain)
2288{
2289 enter_symbol_lookup tmp;
2290
2291 struct symbol *sym;
2292
2293 if (symbol_lookup_debug)
2294 {
2295 struct objfile *objfile
2296 = block == nullptr ? nullptr : block->objfile ();
2297
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",
2302 domain_name (domain).c_str ());
2303 }
2304
2305 lookup_name_info lookup_name (name, match_type);
2306 sym = block_lookup_symbol (block, lookup_name, domain);
2307 if (sym)
2308 {
2309 symbol_lookup_debug_printf_v ("lookup_symbol_in_block (...) = %s",
2310 host_address_to_string (sym));
2311 return sym;
2312 }
2313
2314 symbol_lookup_debug_printf_v ("lookup_symbol_in_block (...) = NULL");
2315 return NULL;
2316}
2317
2318/* See symtab.h. */
2319
2320struct block_symbol
2321lookup_global_symbol_from_objfile (struct objfile *main_objfile,
2322 enum block_enum block_index,
2323 const char *name,
2324 const domain_search_flags domain)
2325{
2326 enter_symbol_lookup tmp;
2327
2328 gdb_assert (block_index == GLOBAL_BLOCK || block_index == STATIC_BLOCK);
2329
2330 for (objfile *objfile : main_objfile->separate_debug_objfiles ())
2331 {
2332 struct block_symbol result
2333 = lookup_symbol_in_objfile (objfile, block_index, name, domain);
2334
2335 if (result.symbol != nullptr)
2336 return result;
2337 }
2338
2339 return {};
2340}
2341
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,
2344 depending on whether or not we want to search global symbols or
2345 static symbols. */
2346
2347static struct block_symbol
2348lookup_symbol_in_objfile_symtabs (struct objfile *objfile,
2349 enum block_enum block_index, const char *name,
2350 const domain_search_flags domain)
2351{
2352 gdb_assert (block_index == GLOBAL_BLOCK || block_index == STATIC_BLOCK);
2353
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",
2358 name, domain_name (domain).c_str ());
2359
2360 struct block_symbol other;
2361 other.symbol = NULL;
2362 for (compunit_symtab *cust : objfile->compunits ())
2363 {
2364 const struct blockvector *bv;
2365 const struct block *block;
2366 struct block_symbol result;
2367
2368 bv = cust->blockvector ();
2369 block = bv->block (block_index);
2370 result.symbol = block_lookup_symbol_primary (block, name, domain);
2371 result.block = block;
2372 if (result.symbol == NULL)
2373 continue;
2374 if (best_symbol (result.symbol, domain))
2375 {
2376 other = result;
2377 break;
2378 }
2379 if (result.symbol->matches (domain))
2380 {
2381 struct symbol *better
2382 = better_symbol (other.symbol, result.symbol, domain);
2383 if (better != other.symbol)
2384 {
2385 other.symbol = better;
2386 other.block = block;
2387 }
2388 }
2389 }
2390
2391 if (other.symbol != NULL)
2392 {
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));
2397 return other;
2398 }
2399
2400 symbol_lookup_debug_printf_v
2401 ("lookup_symbol_in_objfile_symtabs (...) = NULL");
2402 return {};
2403}
2404
2405/* Wrapper around lookup_symbol_in_objfile_symtabs for search_symbols.
2406 Look up LINKAGE_NAME in DOMAIN in the global and static blocks of OBJFILE
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
2412 call us for the objfile that contains a matching minsym. */
2413
2414static struct block_symbol
2415lookup_symbol_in_objfile_from_linkage_name (struct objfile *objfile,
2416 const char *linkage_name,
2417 domain_search_flags domain)
2418{
2419 enum language lang = current_language->la_language;
2420 struct objfile *main_objfile;
2421
2422 demangle_result_storage storage;
2423 const char *modified_name = demangle_for_lookup (linkage_name, lang, storage);
2424
2425 if (objfile->separate_debug_objfile_backlink)
2426 main_objfile = objfile->separate_debug_objfile_backlink;
2427 else
2428 main_objfile = objfile;
2429
2430 for (::objfile *cur_objfile : main_objfile->separate_debug_objfiles ())
2431 {
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)
2440 return result;
2441 }
2442
2443 return {};
2444}
2445
2446/* A helper function that throws an exception when a symbol was found
2447 in a psymtab but not in a symtab. */
2448
2449[[noreturn]] static void
2450error_in_psymtab_expansion (enum block_enum block_index, const char *name,
2451 struct compunit_symtab *cust)
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>)."),
2457 block_index == GLOBAL_BLOCK ? "global" : "static",
2458 name,
2459 symtab_to_filename_for_display (cust->primary_filetab ()),
2460 name, name);
2461}
2462
2463/* A helper function for various lookup routines that interfaces with
2464 the "quick" symbol table functions. */
2465
2466static struct block_symbol
2467lookup_symbol_via_quick_fns (struct objfile *objfile,
2468 enum block_enum block_index, const char *name,
2469 const domain_search_flags domain)
2470{
2471 struct compunit_symtab *cust;
2472 const struct blockvector *bv;
2473 const struct block *block;
2474 struct block_symbol result;
2475
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",
2480 name, domain_name (domain).c_str ());
2481
2482 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
2483 cust = objfile->lookup_symbol (block_index, lookup_name, domain);
2484 if (cust == NULL)
2485 {
2486 symbol_lookup_debug_printf_v
2487 ("lookup_symbol_via_quick_fns (...) = NULL");
2488 return {};
2489 }
2490
2491 bv = cust->blockvector ();
2492 block = bv->block (block_index);
2493 result.symbol = block_lookup_symbol (block, lookup_name, domain);
2494 if (result.symbol == NULL)
2495 error_in_psymtab_expansion (block_index, name, cust);
2496
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));
2501
2502 result.block = block;
2503 return result;
2504}
2505
2506/* See language.h. */
2507
2508struct block_symbol
2509language_defn::lookup_symbol_nonlocal (const char *name,
2510 const struct block *block,
2511 const domain_search_flags domain) const
2512{
2513 struct block_symbol result;
2514
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
2519 result = lookup_symbol_in_static_block (name, block, domain);
2520 if (result.symbol != NULL)
2521 return result;
2522
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". */
2529 if ((domain & SEARCH_TYPE_DOMAIN) != 0)
2530 {
2531 struct gdbarch *gdbarch;
2532
2533 if (block == NULL)
2534 gdbarch = current_inferior ()->arch ();
2535 else
2536 gdbarch = block->gdbarch ();
2537 result.symbol = language_lookup_primitive_type_as_symbol (this,
2538 gdbarch, name);
2539 result.block = NULL;
2540 if (result.symbol != NULL)
2541 return result;
2542 }
2543
2544 return lookup_global_symbol (name, block, domain);
2545}
2546
2547/* See symtab.h. */
2548
2549struct block_symbol
2550lookup_symbol_in_static_block (const char *name,
2551 const struct block *block,
2552 const domain_search_flags domain)
2553{
2554 if (block == nullptr)
2555 return {};
2556
2557 const struct block *static_block = block->static_block ();
2558 struct symbol *sym;
2559
2560 if (static_block == NULL)
2561 return {};
2562
2563 if (symbol_lookup_debug)
2564 {
2565 struct objfile *objfile = (block == nullptr
2566 ? nullptr : block->objfile ());
2567
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",
2572 domain_name (domain).c_str ());
2573 }
2574
2575 sym = lookup_symbol_in_block (name,
2576 symbol_name_match_type::FULL,
2577 static_block, domain);
2578 symbol_lookup_debug_printf ("lookup_symbol_in_static_block (...) = %s",
2579 sym != NULL
2580 ? host_address_to_string (sym) : "NULL");
2581 return (struct block_symbol) {sym, static_block};
2582}
2583
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
2589static struct block_symbol
2590lookup_symbol_in_objfile (struct objfile *objfile, enum block_enum block_index,
2591 const char *name, const domain_search_flags domain)
2592{
2593 struct block_symbol result;
2594
2595 gdb_assert (block_index == GLOBAL_BLOCK || block_index == STATIC_BLOCK);
2596
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",
2601 name, domain_name (domain).c_str ());
2602
2603 result = lookup_symbol_in_objfile_symtabs (objfile, block_index,
2604 name, domain);
2605 if (result.symbol != NULL)
2606 {
2607 symbol_lookup_debug_printf
2608 ("lookup_symbol_in_objfile (...) = %s (in symtabs)",
2609 host_address_to_string (result.symbol));
2610 return result;
2611 }
2612
2613 result = lookup_symbol_via_quick_fns (objfile, block_index,
2614 name, domain);
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 : "");
2621 return result;
2622}
2623
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. */
2627
2628static struct block_symbol
2629lookup_global_or_static_symbol (const char *name,
2630 enum block_enum block_index,
2631 struct objfile *objfile,
2632 const domain_search_flags domain)
2633{
2634 struct symbol_cache *cache = get_symbol_cache (current_program_space);
2635 struct block_symbol result;
2636 struct block_symbol_cache *bsc;
2637 struct symbol_cache_slot *slot;
2638
2639 gdb_assert (block_index == GLOBAL_BLOCK || block_index == STATIC_BLOCK);
2640 gdb_assert (objfile == nullptr || block_index == GLOBAL_BLOCK);
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. */
2644 result = symbol_cache_lookup (cache, objfile, block_index, name, domain,
2645 &bsc, &slot);
2646 if (result.symbol != NULL)
2647 {
2648 if (SYMBOL_LOOKUP_FAILED_P (result))
2649 return {};
2650 return result;
2651 }
2652
2653 enter_symbol_lookup tmp;
2654
2655 /* Do a global search (of global blocks, heh). */
2656 if (result.symbol == NULL)
2657 gdbarch_iterate_over_objfiles_in_search_order
2658 (objfile != NULL ? objfile->arch () : current_inferior ()->arch (),
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);
2666
2667 if (result.symbol != NULL)
2668 symbol_cache_mark_found (bsc, slot, objfile, result.symbol, result.block,
2669 domain);
2670 else
2671 symbol_cache_mark_not_found (bsc, slot, objfile, name, domain);
2672
2673 return result;
2674}
2675
2676/* See symtab.h. */
2677
2678struct block_symbol
2679lookup_static_symbol (const char *name, const domain_search_flags domain)
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,
2689 const domain_search_flags domain)
2690{
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. */
2694 const struct block *global_block
2695 = block == nullptr ? nullptr : block->global_block ();
2696 symbol *sym = NULL;
2697 if (global_block != nullptr)
2698 {
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))
2703 return { sym, global_block };
2704 }
2705
2706 struct objfile *objfile = nullptr;
2707 if (block != nullptr)
2708 {
2709 objfile = block->objfile ();
2710 if (objfile->separate_debug_objfile_backlink != nullptr)
2711 objfile = objfile->separate_debug_objfile_backlink;
2712 }
2713
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;
2720}
2721
2722/* See symtab.h. */
2723
2724bool
2725symbol::matches (domain_search_flags flags) const
2726{
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;
2731
2732 return search_flags_matches (flags, m_domain);
2733}
2734
2735/* See symtab.h. */
2736
2737struct type *
2738lookup_transparent_type (const char *name, domain_search_flags flags)
2739{
2740 return current_language->lookup_transparent_type (name, flags);
2741}
2742
2743/* A helper for basic_lookup_transparent_type that interfaces with the
2744 "quick" symbol table functions. */
2745
2746static struct type *
2747basic_lookup_transparent_type_quick (struct objfile *objfile,
2748 enum block_enum block_index,
2749 domain_search_flags flags,
2750 const lookup_name_info &name)
2751{
2752 struct compunit_symtab *cust;
2753 const struct blockvector *bv;
2754 const struct block *block;
2755 struct symbol *sym;
2756
2757 cust = objfile->lookup_symbol (block_index, name, flags);
2758 if (cust == NULL)
2759 return NULL;
2760
2761 bv = cust->blockvector ();
2762 block = bv->block (block_index);
2763
2764 sym = block_find_symbol (block, name, flags, nullptr);
2765 if (sym == nullptr)
2766 error_in_psymtab_expansion (block_index, name.c_str (), cust);
2767 gdb_assert (!TYPE_IS_OPAQUE (sym->type ()));
2768 return sym->type ();
2769}
2770
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 *
2776basic_lookup_transparent_type_1 (struct objfile *objfile,
2777 enum block_enum block_index,
2778 domain_search_flags flags,
2779 const lookup_name_info &name)
2780{
2781 const struct blockvector *bv;
2782 const struct block *block;
2783 const struct symbol *sym;
2784
2785 for (compunit_symtab *cust : objfile->compunits ())
2786 {
2787 bv = cust->blockvector ();
2788 block = bv->block (block_index);
2789 sym = block_find_symbol (block, name, flags, nullptr);
2790 if (sym != nullptr)
2791 {
2792 gdb_assert (!TYPE_IS_OPAQUE (sym->type ()));
2793 return sym->type ();
2794 }
2795 }
2796
2797 return NULL;
2798}
2799
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
2803 types are available in STRUCT_DOMAIN and only in file-static or
2804 global blocks. */
2805
2806struct type *
2807basic_lookup_transparent_type (const char *name, domain_search_flags flags)
2808{
2809 struct type *t;
2810
2811 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
2812
2813 /* Now search all the global symbols. Do the symtab's first, then
2814 check the psymtab's. If a psymtab indicates the existence
2815 of the desired name as a global, then do psymtab-to-symtab
2816 conversion on the fly and return the found symbol. */
2817
2818 for (objfile *objfile : current_program_space->objfiles ())
2819 {
2820 t = basic_lookup_transparent_type_1 (objfile, GLOBAL_BLOCK,
2821 flags, lookup_name);
2822 if (t)
2823 return t;
2824 }
2825
2826 for (objfile *objfile : current_program_space->objfiles ())
2827 {
2828 t = basic_lookup_transparent_type_quick (objfile, GLOBAL_BLOCK,
2829 flags, lookup_name);
2830 if (t)
2831 return t;
2832 }
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
2837 check the psymtab's. If a psymtab indicates the existence
2838 of the desired name as a file-level static, then do psymtab-to-symtab
2839 conversion on the fly and return the found symbol. */
2840
2841 for (objfile *objfile : current_program_space->objfiles ())
2842 {
2843 t = basic_lookup_transparent_type_1 (objfile, STATIC_BLOCK,
2844 flags, lookup_name);
2845 if (t)
2846 return t;
2847 }
2848
2849 for (objfile *objfile : current_program_space->objfiles ())
2850 {
2851 t = basic_lookup_transparent_type_quick (objfile, STATIC_BLOCK,
2852 flags, lookup_name);
2853 if (t)
2854 return t;
2855 }
2856
2857 return (struct type *) 0;
2858}
2859
2860/* See symtab.h. */
2861
2862bool
2863iterate_over_symbols (const struct block *block,
2864 const lookup_name_info &name,
2865 const domain_search_flags domain,
2866 gdb::function_view<symbol_found_callback_ftype> callback)
2867{
2868 for (struct symbol *sym : block_iterator_range (block, &name))
2869 {
2870 if (sym->matches (domain))
2871 {
2872 struct block_symbol block_sym = {sym, block};
2873
2874 if (!callback (&block_sym))
2875 return false;
2876 }
2877 }
2878 return true;
2879}
2880
2881/* See symtab.h. */
2882
2883bool
2884iterate_over_symbols_terminated
2885 (const struct block *block,
2886 const lookup_name_info &name,
2887 const domain_search_flags domain,
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
2896/* Find the compunit symtab associated with PC and SECTION.
2897 This will read in debug info as necessary. */
2898
2899struct compunit_symtab *
2900find_pc_sect_compunit_symtab (CORE_ADDR pc, struct obj_section *section)
2901{
2902 struct compunit_symtab *best_cust = NULL;
2903 CORE_ADDR best_cust_range = 0;
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. */
2910 bound_minimal_symbol msymbol
2911 = lookup_minimal_symbol_by_pc_section (pc, section);
2912 if (msymbol.minsym && msymbol.minsym->data_p ())
2913 return NULL;
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
2922 gets its own symtab. The symtab for the included file should have
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
2930 for (objfile *obj_file : current_program_space->objfiles ())
2931 {
2932 for (compunit_symtab *cust : obj_file->compunits ())
2933 {
2934 const struct blockvector *bv = cust->blockvector ();
2935 const struct block *global_block = bv->global_block ();
2936 CORE_ADDR start = global_block->start ();
2937 CORE_ADDR end = global_block->end ();
2938 bool in_range_p = start <= pc && pc < end;
2939 if (!in_range_p)
2940 continue;
2941
2942 if (bv->map () != nullptr)
2943 {
2944 if (bv->map ()->find (pc) == nullptr)
2945 continue;
2946
2947 return cust;
2948 }
2949
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. */
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;
2967
2968 if (section != 0)
2969 {
2970 struct symbol *found_sym = nullptr;
2971
2972 for (int b_index = GLOBAL_BLOCK;
2973 b_index <= STATIC_BLOCK && found_sym == nullptr;
2974 ++b_index)
2975 {
2976 const struct block *b = bv->block (b_index);
2977 for (struct symbol *sym : block_iterator_range (b))
2978 {
2979 if (matching_obj_sections (sym->obj_section (obj_file),
2980 section))
2981 {
2982 found_sym = sym;
2983 break;
2984 }
2985 }
2986 }
2987 if (found_sym == nullptr)
2988 continue; /* No symbol in this symtab matches
2989 section. */
2990 }
2991
2992 /* Cust is best found so far, save it. */
2993 best_cust = cust;
2994 best_cust_range = range;
2995 }
2996 }
2997
2998 if (best_cust != NULL)
2999 return best_cust;
3000
3001 /* Not found in symtabs, search the "quick" symtabs (e.g. psymtabs). */
3002
3003 for (objfile *objf : current_program_space->objfiles ())
3004 {
3005 struct compunit_symtab *result
3006 = objf->find_pc_sect_compunit_symtab (msymbol, pc, section, 1);
3007 if (result != NULL)
3008 return result;
3009 }
3010
3011 return NULL;
3012}
3013
3014/* Find the compunit symtab associated with PC.
3015 This will read in debug info as necessary.
3016 Backward compatibility, no section. */
3017
3018struct compunit_symtab *
3019find_pc_compunit_symtab (CORE_ADDR pc)
3020{
3021 return find_pc_sect_compunit_symtab (pc, find_pc_mapped_section (pc));
3022}
3023
3024/* See symtab.h. */
3025
3026struct symbol *
3027find_symbol_at_address (CORE_ADDR address)
3028{
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 *
3032 {
3033 const struct blockvector *bv = symtab->blockvector ();
3034
3035 for (int i = GLOBAL_BLOCK; i <= STATIC_BLOCK; ++i)
3036 {
3037 const struct block *b = bv->block (i);
3038
3039 for (struct symbol *sym : block_iterator_range (b))
3040 {
3041 if (sym->aclass () == LOC_STATIC
3042 && sym->value_address () == addr)
3043 return sym;
3044 }
3045 }
3046 return nullptr;
3047 };
3048
3049 for (objfile *objfile : current_program_space->objfiles ())
3050 {
3051 /* If this objfile was read with -readnow, then we need to
3052 search the symtabs directly. */
3053 if ((objfile->flags & OBJF_READNOW) != 0)
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
3065 = objfile->find_compunit_symtab_by_address (address);
3066 if (symtab != NULL)
3067 {
3068 struct symbol *sym = search_symtab (symtab, address);
3069 if (sym != nullptr)
3070 return sym;
3071 }
3072 }
3073 }
3074
3075 return NULL;
3076}
3077
3078\f
3079
3080/* Find the source file and line number for a given PC value and SECTION.
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
3095struct symtab_and_line
3096find_pc_sect_line (CORE_ADDR pc, struct obj_section *section, int notcurrent)
3097{
3098 struct compunit_symtab *cust;
3099 const linetable *l;
3100 int len;
3101 const linetable_entry *item;
3102 const struct blockvector *bv;
3103
3104 /* Info on best line seen so far, and where it starts, and its file. */
3105
3106 const linetable_entry *best = NULL;
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. */
3115 const linetable_entry *alt = NULL;
3116
3117 /* Info on best line seen in this file. */
3118
3119 const linetable_entry *prev;
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
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. */
3133
3134 if (notcurrent)
3135 pc -= 1;
3136
3137 /* elz: added this because this function returned the wrong
3138 information if the pc belongs to a stub (import/export)
3139 to call a shlib function. This stub would be anywhere between
3140 two functions in the target, and the line info was erroneously
3141 taken to be the one of the line before the pc. */
3142
3143 /* RT: Further explanation:
3144
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
3151 * sorted by start address. The stubs are marked as "trampoline",
3152 * the others appear as text. E.g.:
3153 *
3154 * Minimal symbol table for main image
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
3171 * if we're really in the trampoline.s If we're beyond it (say
3172 * we're in "foo" in the above example), it'll have a closer
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
3177 * be different than the trampoline address. I put in a sanity
3178 * check for the address being the same, to avoid an
3179 * infinite recursion.
3180 */
3181 bound_minimal_symbol msymbol = lookup_minimal_symbol_by_pc (pc);
3182 if (msymbol.minsym != NULL)
3183 if (msymbol.minsym->type () == mst_solib_trampoline)
3184 {
3185 bound_minimal_symbol mfunsym
3186 = lookup_minimal_symbol_text (current_program_space,
3187 msymbol.minsym->linkage_name (),
3188 nullptr);
3189
3190 if (mfunsym.minsym == NULL)
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 ...
3196 * In the above situation, the shared lib is not loaded yet,
3197 * so of course we can't find the real func/line info,
3198 * but the "break" still works, and the warning is annoying.
3199 * So I commented out the warning. RT */
3200 /* warning ("In stub for %s; unable to find real function/line info",
3201 msymbol->linkage_name ()); */
3202 ;
3203 /* fall through */
3204 else if (mfunsym.value_address ()
3205 == msymbol.value_address ())
3206 /* Avoid infinite recursion */
3207 /* See above comment about why warning is commented out. */
3208 /* warning ("In stub for %s; unable to find real function/line info",
3209 msymbol->linkage_name ()); */
3210 ;
3211 /* fall through */
3212 else
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. */
3217 if (mfunsym.value_address () == pc)
3218 internal_error (_("Infinite recursion detected in find_pc_sect_line;"
3219 "please file a bug report"));
3220
3221 return find_pc_line (mfunsym.value_address (), 0);
3222 }
3223 }
3224
3225 symtab_and_line val;
3226 val.pspace = current_program_space;
3227
3228 cust = find_pc_sect_compunit_symtab (pc, section);
3229 if (cust == NULL)
3230 {
3231 /* If no symbol information, return previous pc. */
3232 if (notcurrent)
3233 pc++;
3234 val.pc = pc;
3235 return val;
3236 }
3237
3238 bv = cust->blockvector ();
3239 struct objfile *objfile = cust->objfile ();
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
3245 for (symtab *iter_s : cust->filetabs ())
3246 {
3247 /* Find the best line in this symtab. */
3248 l = iter_s->linetable ();
3249 if (!l)
3250 continue;
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;
3262 item = l->item; /* Get first line info. */
3263
3264 /* Is this file's first line closer than the first lines of other files?
3265 If so, record this file, and its first line, as best alternate. */
3266 if (item->pc (objfile) > pc
3267 && (!alt || item->unrelocated_pc () < alt->unrelocated_pc ()))
3268 alt = item;
3269
3270 auto pc_compare = [] (const unrelocated_addr &comp_pc,
3271 const struct linetable_entry & lhs)
3272 {
3273 return comp_pc < lhs.unrelocated_pc ();
3274 };
3275
3276 const linetable_entry *first = item;
3277 const linetable_entry *last = item + len;
3278 item = (std::upper_bound
3279 (first, last,
3280 unrelocated_addr (pc - objfile->text_section_offset ()),
3281 pc_compare));
3282 if (item != first)
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 }
3290
3291 /* At this point, prev points at the line whose start addr is <= pc, and
3292 item points at the next statement. If we ran off the end of the linetable
3293 (pc >= start of the last line), then prev == item. If pc < start of
3294 the first line, prev will not be set. */
3295
3296 /* Is this file's best line closer than the best in the other files?
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. */
3300
3301 if (prev && prev->line
3302 && (!best || prev->unrelocated_pc () > best->unrelocated_pc ()))
3303 {
3304 best = prev;
3305 best_symtab = iter_s;
3306
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 {
3316 const linetable_entry *tmp = best;
3317 while (tmp > first
3318 && (tmp - 1)->unrelocated_pc () == tmp->unrelocated_pc ()
3319 && (tmp - 1)->line != 0 && !tmp->is_stmt)
3320 --tmp;
3321 if (tmp->is_stmt)
3322 best = tmp;
3323 }
3324
3325 /* Discard BEST_END if it's before the PC of the current BEST. */
3326 if (best_end <= best->pc (objfile))
3327 best_end = 0;
3328 }
3329
3330 /* If another line (denoted by ITEM) is in the linetable and its
3331 PC is after BEST's PC, but before the current BEST_END, then
3332 use ITEM's PC as the new best_end. */
3333 if (best && item < last
3334 && item->unrelocated_pc () > best->unrelocated_pc ()
3335 && (best_end == 0 || best_end > item->pc (objfile)))
3336 best_end = item->pc (objfile);
3337 }
3338
3339 if (!best_symtab)
3340 {
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;
3346 }
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
3351 find any valid line information. */
3352 val.pc = pc;
3353 }
3354 else
3355 {
3356 val.is_stmt = best->is_stmt;
3357 val.symtab = best_symtab;
3358 val.line = best->line;
3359 val.pc = best->pc (objfile);
3360 if (best_end && (!alt || best_end < alt->pc (objfile)))
3361 val.end = best_end;
3362 else if (alt)
3363 val.end = alt->pc (objfile);
3364 else
3365 val.end = bv->global_block ()->end ();
3366 }
3367 val.section = section;
3368 return val;
3369}
3370
3371/* Backward compatibility (no section). */
3372
3373struct symtab_and_line
3374find_pc_line (CORE_ADDR pc, int notcurrent)
3375{
3376 struct obj_section *section;
3377
3378 section = find_pc_overlay (pc);
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;
3391}
3392
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
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}
3454\f
3455/* See symtab.h. */
3456
3457symtab *
3458find_line_symtab (symtab *sym_tab, int line, int *index)
3459{
3460 int exact = 0; /* Initialized here to avoid a compiler warning. */
3461
3462 /* BEST_INDEX and BEST_LINETABLE identify the smallest linenumber > LINE
3463 so far seen. */
3464
3465 int best_index;
3466 const struct linetable *best_linetable;
3467 struct symtab *best_symtab;
3468
3469 /* First try looking it up in the given symtab. */
3470 best_linetable = sym_tab->linetable ();
3471 best_symtab = sym_tab;
3472 best_index = find_line_common (best_linetable, line, &exact, 0);
3473 if (best_index < 0 || !exact)
3474 {
3475 /* Didn't find an exact match. So we better keep looking for
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). */
3481
3482 /* BEST is the smallest linenumber > LINE so far seen,
3483 or 0 if none has been seen so far.
3484 BEST_INDEX and BEST_LINETABLE identify the item for it. */
3485 int best;
3486
3487 if (best_index >= 0)
3488 best = best_linetable->item[best_index].line;
3489 else
3490 best = 0;
3491
3492 for (objfile *objfile : current_program_space->objfiles ())
3493 objfile->expand_symtabs_with_fullname (symtab_to_fullname (sym_tab));
3494
3495 for (objfile *objfile : current_program_space->objfiles ())
3496 {
3497 for (compunit_symtab *cu : objfile->compunits ())
3498 {
3499 for (symtab *s : cu->filetabs ())
3500 {
3501 const struct linetable *l;
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;
3509 l = s->linetable ();
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 }
3531 }
3532done:
3533 if (best_index < 0)
3534 return NULL;
3535
3536 if (index)
3537 *index = best_index;
3538
3539 return best_symtab;
3540}
3541
3542/* Given SYMTAB, returns all the PCs function in the symtab that
3543 exactly match LINE. Returns an empty vector if there are no exact
3544 matches, but updates BEST_ITEM in this case. */
3545
3546std::vector<CORE_ADDR>
3547find_pcs_for_symtab_line (struct symtab *symtab, int line,
3548 const linetable_entry **best_item)
3549{
3550 int start = 0;
3551 std::vector<CORE_ADDR> result;
3552 struct objfile *objfile = symtab->compunit ()->objfile ();
3553
3554 /* First, collect all the PCs that are at this line. */
3555 while (1)
3556 {
3557 int was_exact;
3558 int idx;
3559
3560 idx = find_line_common (symtab->linetable (), line, &was_exact,
3561 start);
3562 if (idx < 0)
3563 break;
3564
3565 if (!was_exact)
3566 {
3567 const linetable_entry *item = &symtab->linetable ()->item[idx];
3568
3569 if (*best_item == NULL
3570 || (item->line < (*best_item)->line && item->is_stmt))
3571 *best_item = item;
3572
3573 break;
3574 }
3575
3576 result.push_back (symtab->linetable ()->item[idx].pc (objfile));
3577 start = idx + 1;
3578 }
3579
3580 return result;
3581}
3582
3583\f
3584/* Set the PC value for a given source file and line number and return true.
3585 Returns false for invalid line number (and sets the PC to 0).
3586 The source file is specified with a struct symtab. */
3587
3588bool
3589find_line_pc (struct symtab *symtab, int line, CORE_ADDR *pc)
3590{
3591 const struct linetable *l;
3592 int ind;
3593
3594 *pc = 0;
3595 if (symtab == 0)
3596 return false;
3597
3598 symtab = find_line_symtab (symtab, line, &ind);
3599 if (symtab != NULL)
3600 {
3601 l = symtab->linetable ();
3602 *pc = l->item[ind].pc (symtab->compunit ()->objfile ());
3603 return true;
3604 }
3605 else
3606 return false;
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.
3612 Returns true to indicate success.
3613 Returns false if could not find the specified line. */
3614
3615bool
3616find_line_pc_range (struct symtab_and_line sal, CORE_ADDR *startptr,
3617 CORE_ADDR *endptr)
3618{
3619 CORE_ADDR startaddr;
3620 struct symtab_and_line found_sal;
3621
3622 startaddr = sal.pc;
3623 if (startaddr == 0 && !find_line_pc (sal.symtab, sal.line, &startaddr))
3624 return false;
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 }
3645 return true;
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.
3651 START is the index at which to start searching the line table.
3652
3653 Set *EXACT_MATCH nonzero if the value returned is an exact match. */
3654
3655static int
3656find_line_common (const linetable *l, int lineno,
3657 int *exact_match, int start)
3658{
3659 int i;
3660 int len;
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
3669 *exact_match = 0;
3670
3671 if (lineno <= 0)
3672 return -1;
3673 if (l == 0)
3674 return -1;
3675
3676 len = l->nitems;
3677 for (i = start; i < len; i++)
3678 {
3679 const linetable_entry *item = &(l->item[i]);
3680
3681 /* Ignore non-statements. */
3682 if (!item->is_stmt)
3683 continue;
3684
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. */
3700 return best_index;
3701}
3702
3703bool
3704find_pc_line_pc_range (CORE_ADDR pc, CORE_ADDR *startptr, CORE_ADDR *endptr)
3705{
3706 struct symtab_and_line sal;
3707
3708 sal = find_pc_line (pc, 0);
3709 *startptr = sal.pc;
3710 *endptr = sal.end;
3711 return sal.symtab != 0;
3712}
3713
3714/* Helper for find_function_start_sal. Does most of the work, except
3715 setting the sal's symbol. */
3716
3717static symtab_and_line
3718find_function_start_sal_1 (CORE_ADDR func_addr, obj_section *section,
3719 bool funfirstline)
3720{
3721 symtab_and_line sal = find_pc_sect_line (func_addr, section, 0);
3722
3723 if (funfirstline && sal.symtab != NULL
3724 && (sal.symtab->compunit ()->locations_valid ()
3725 || sal.symtab->language () == language_asm))
3726 {
3727 struct gdbarch *gdbarch = sal.symtab->compunit ()->objfile ()->arch ();
3728
3729 sal.pc = func_addr;
3730 if (gdbarch_skip_entrypoint_p (gdbarch))
3731 sal.pc = gdbarch_skip_entrypoint (gdbarch, sal.pc);
3732 return sal;
3733 }
3734
3735 /* We always should have a line for the function start address.
3736 If we don't, something is odd. Create a plain SAL referring
3737 just the PC and hope that skip_prologue_sal (if requested)
3738 can find a line number for after the prologue. */
3739 if (sal.pc < func_addr)
3740 {
3741 sal = {};
3742 sal.pspace = current_program_space;
3743 sal.pc = func_addr;
3744 sal.section = section;
3745 }
3746
3747 if (funfirstline)
3748 skip_prologue_sal (&sal);
3749
3750 return sal;
3751}
3752
3753/* See symtab.h. */
3754
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
3772symtab_and_line
3773find_function_start_sal (symbol *sym, bool funfirstline)
3774{
3775 symtab_and_line sal
3776 = find_function_start_sal_1 (sym->value_block ()->entry_pc (),
3777 sym->obj_section (sym->objfile ()),
3778 funfirstline);
3779 sal.symbol = sym;
3780 return sal;
3781}
3782
3783
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. */
3788
3789static CORE_ADDR
3790skip_prologue_using_lineinfo (CORE_ADDR func_addr, struct symtab *symtab)
3791{
3792 CORE_ADDR func_start, func_end;
3793 const struct linetable *l;
3794 int i;
3795
3796 /* Give up if this symbol has no lineinfo table. */
3797 l = symtab->linetable ();
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
3806 struct objfile *objfile = symtab->compunit ()->objfile ();
3807
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 {
3814 const linetable_entry *item = &(l->item[i]);
3815 CORE_ADDR item_pc = item->pc (objfile);
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. */
3820 if (item->line > 0 && func_start <= item_pc && item_pc < func_end)
3821 return item_pc;
3822 }
3823
3824 return func_addr;
3825}
3826
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
3834static std::optional<CORE_ADDR>
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 {
3846 const linetable *linetable = prologue_sal.symtab->linetable ();
3847
3848 struct objfile *objfile = prologue_sal.symtab->compunit ()->objfile ();
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 ());
3854
3855 auto it = std::lower_bound
3856 (linetable->item, linetable->item + linetable->nitems, unrel_start,
3857 [] (const linetable_entry &lte, unrelocated_addr pc)
3858 {
3859 return lte.unrelocated_pc () < pc;
3860 });
3861
3862 for (;
3863 (it < linetable->item + linetable->nitems
3864 && it->unrelocated_pc () < unrel_end);
3865 it++)
3866 if (it->prologue_end)
3867 return {it->pc (objfile)};
3868 }
3869
3870 return {};
3871}
3872
3873/* Adjust SAL to the first instruction past the function prologue.
3874 If the PC was explicitly specified, the SAL is not changed.
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. */
3879
3880void
3881skip_prologue_sal (struct symtab_and_line *sal)
3882{
3883 struct symbol *sym;
3884 struct symtab_and_line start_sal;
3885 CORE_ADDR pc, saved_pc;
3886 struct obj_section *section;
3887 const char *name;
3888 struct objfile *objfile;
3889 struct gdbarch *gdbarch;
3890 const struct block *b, *function_block;
3891 int force_skip, skip;
3892
3893 /* Do not change the SAL if PC was specified explicitly. */
3894 if (sal->explicit_pc)
3895 return;
3896
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
3903 && sal->symtab->language () == language_asm)
3904 return;
3905
3906 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3907
3908 switch_to_program_space_and_thread (sal->pspace);
3909
3910 sym = find_pc_sect_function (sal->pc, sal->section);
3911 if (sym != NULL)
3912 {
3913 objfile = sym->objfile ();
3914 pc = sym->value_block ()->entry_pc ();
3915 section = sym->obj_section (objfile);
3916 name = sym->linkage_name ();
3917 }
3918 else
3919 {
3920 bound_minimal_symbol msymbol
3921 = lookup_minimal_symbol_by_pc_section (sal->pc, sal->section);
3922
3923 if (msymbol.minsym == NULL)
3924 return;
3925
3926 objfile = msymbol.objfile;
3927 pc = msymbol.value_address ();
3928 section = msymbol.minsym->obj_section (objfile);
3929 name = msymbol.minsym->linkage_name ();
3930 }
3931
3932 gdbarch = objfile->arch ();
3933
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). */
3938
3939 skip = 1;
3940 force_skip = 1;
3941
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. */
3945 if (sym != NULL
3946 && sym->symtab ()->compunit ()->locations_valid ())
3947 force_skip = 0;
3948
3949 saved_pc = pc;
3950 do
3951 {
3952 pc = saved_pc;
3953
3954 /* Check if the compiler explicitly indicated where a breakpoint should
3955 be placed to skip the prologue. */
3956 if (!ignore_prologue_end_flag && skip)
3957 {
3958 std::optional<CORE_ADDR> linetable_pc
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
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);
3976 if (gdbarch_skip_entrypoint_p (gdbarch))
3977 pc = gdbarch_skip_entrypoint (gdbarch, pc);
3978 if (skip)
3979 pc = gdbarch_skip_prologue_noexcept (gdbarch, pc);
3980
3981 /* For overlays, map pc back into its mapped VMA range. */
3982 pc = overlay_mapped_address (pc, section);
3983
3984 /* Calculate line number. */
3985 start_sal = find_pc_sect_line (pc, section, 0);
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
3990 && (sym ? (sym->value_block ()->entry_pc () <= start_sal.end
3991 && start_sal.end < sym->value_block()->end ())
3992 : (lookup_minimal_symbol_by_pc_section (start_sal.end, section).minsym
3993 == lookup_minimal_symbol_by_pc_section (pc, section).minsym)))
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)
4006 && name && strcmp_iw (name, "main") == 0)
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 }
4013 }
4014 while (!force_skip && skip--);
4015
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. */
4024 if (!force_skip && sym && start_sal.symtab == NULL)
4025 {
4026 pc = skip_prologue_using_lineinfo (pc, sym->symtab ());
4027 /* Recalculate the line number. */
4028 start_sal = find_pc_sect_line (pc, section, 0);
4029 }
4030
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;
4038 sal->symtab = start_sal.symtab;
4039 sal->line = start_sal.line;
4040 sal->end = start_sal.end;
4041
4042 /* Check if we are now inside an inlined function. If we can,
4043 use the call site of the function instead. */
4044 b = block_for_pc_sect (sal->pc, sal->section);
4045 function_block = NULL;
4046 while (b != NULL)
4047 {
4048 if (b->function () != NULL && b->inlined_p ())
4049 function_block = b;
4050 else if (b->function () != NULL)
4051 break;
4052 b = b->superblock ();
4053 }
4054 if (function_block != NULL
4055 && function_block->function ()->line () != 0)
4056 {
4057 sal->line = function_block->function ()->line ();
4058 sal->symtab = function_block->function ()->symtab ();
4059 }
4060}
4061
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. */
4099 if (prologue_sal.symtab->language () != language_asm)
4100 {
4101 struct objfile *objfile
4102 = prologue_sal.symtab->compunit ()->objfile ();
4103 const linetable *linetable = prologue_sal.symtab->linetable ();
4104 gdb_assert (linetable->nitems > 0);
4105 int idx = 0;
4106
4107 /* Skip any earlier lines, and any end-of-sequence marker
4108 from a previous function. */
4109 while (idx + 1 < linetable->nitems
4110 && (linetable->item[idx].pc (objfile) != prologue_sal.pc
4111 || linetable->item[idx].line == 0))
4112 idx++;
4113
4114 if (idx + 1 < linetable->nitems
4115 && linetable->item[idx+1].line != 0
4116 && linetable->item[idx+1].pc (objfile) == start_pc)
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 {
4148 if (bl->inlined_p ())
4149 break;
4150 if (bl->function ())
4151 {
4152 bl = NULL;
4153 break;
4154 }
4155 bl = bl->superblock ();
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}
4179
4180/* See symtab.h. */
4181
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
4190 /* While the standard allows for multiple points marked with epilogue_begin
4191 in the same function, for performance reasons, this function will only
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);
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 ();
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. */
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 });
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 }
4244
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 {
4258 /* Doing it-- would introduce undefined behavior, avoid it by
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.
4292 Doing it-- would introduce undefined behavior, avoid it by
4293 explicitly handling this case. */
4294 break;
4295 }
4296 }
4297 }
4298
4299 return {};
4300}
4301
4302/* See symtab.h. */
4303
4304symbol *
4305find_function_alias_target (bound_minimal_symbol msymbol)
4306{
4307 CORE_ADDR func_addr;
4308 if (!msymbol_is_function (msymbol.objfile, msymbol.minsym, &func_addr))
4309 return NULL;
4310
4311 symbol *sym = find_pc_function (func_addr);
4312 if (sym != NULL
4313 && sym->aclass () == LOC_BLOCK
4314 && sym->value_block ()->entry_pc () == func_addr)
4315 return sym;
4316
4317 return NULL;
4318}
4319
4320\f
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 "". */
4325
4326static const char *
4327operator_chars (const char *p, const char **end)
4328{
4329 *end = "";
4330 if (!startswith (p, CP_OPERATOR_STR))
4331 return *end;
4332 p += CP_OPERATOR_LEN;
4333
4334 /* Don't get faked out by `operator' being part of a longer
4335 identifier. */
4336 if (isalpha (*p) || *p == '_' || *p == '$' || *p == '\0')
4337 return *end;
4338
4339 /* Allow some whitespace between `operator' and the operator symbol. */
4340 while (*p == ' ' || *p == '\t')
4341 p++;
4342
4343 /* Recognize 'operator TYPENAME'. */
4344
4345 if (isalpha (*p) || *p == '_' || *p == '$')
4346 {
4347 const char *q = p + 1;
4348
4349 while (isalnum (*q) || *q == '_' || *q == '$')
4350 q++;
4351 *end = q;
4352 return p;
4353 }
4354
4355 while (*p)
4356 switch (*p)
4357 {
4358 case '\\': /* regexp quoting */
4359 if (p[1] == '*')
4360 {
4361 if (p[2] == '=') /* 'operator\*=' */
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] == ']')
4370 error (_("mismatched quoting on brackets, "
4371 "try 'operator\\[\\]'"));
4372 else if (p[2] == '\\' && p[3] == ']')
4373 {
4374 *end = p + 4; /* 'operator\[\]' */
4375 return p;
4376 }
4377 else
4378 error (_("nothing is allowed between '[' and ']'"));
4379 }
4380 else
4381 {
4382 /* Gratuitous quote: skip it and move on. */
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 {
4406 /* Struct pointer member operator 'operator->'. */
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 ',':
4430 *end = p + 1;
4431 return p;
4432 case '(':
4433 if (p[1] != ')')
4434 error (_("`operator ()' must be specified "
4435 "without whitespace in `()'"));
4436 *end = p + 2;
4437 return p;
4438 case '?':
4439 if (p[1] != ':')
4440 error (_("`operator ?:' must be specified "
4441 "without whitespace in `?:'"));
4442 *end = p + 2;
4443 return p;
4444 case '[':
4445 if (p[1] != ']')
4446 error (_("`operator []' must be specified "
4447 "without whitespace in `[]'"));
4448 *end = p + 2;
4449 return p;
4450 default:
4451 error (_("`operator %s' not supported"), p);
4452 break;
4453 }
4454
4455 *end = "";
4456 return *end;
4457}
4458\f
4459
4460/* See class declaration. */
4461
4462info_sources_filter::info_sources_filter (match_on match_type,
4463 const char *regexp)
4464 : m_match_type (match_type),
4465 m_regexp (regexp)
4466{
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);
4471
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}
4479
4480/* See class declaration. */
4481
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)
4492 {
4493 case match_on::DIRNAME:
4494 dirname = gdb_ldirname (fullname);
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;
4503 default:
4504 gdb_assert_not_reached ("bad m_match_type");
4505 }
4506
4507 if (m_c_regexp->exec (to_match, 0, NULL, 0) != 0)
4508 return false;
4509 }
4510
4511 return true;
4512}
4513
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'
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)
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
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);
4546
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 {
4551 /* The false here indicates that this file is from an unexpanded
4552 symtab. */
4553 output (filename, fullname, false);
4554 }
4555
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
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;
4574
4575 /* The object to which output is sent. */
4576 struct ui_out *m_uiout;
4577};
4578
4579/* See comment in class declaration above. */
4580
4581void
4582output_source_filename_data::output (const char *disp_name,
4583 const char *fullname,
4584 bool expanded_p)
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
4595 /* Was NAME already seen? If so, then don't print it again. */
4596 if (m_filename_seen_cache.seen (fullname))
4597 return;
4598
4599 /* If the filter rejects this file then don't print it. */
4600 if (!m_filter.matches (fullname))
4601 return;
4602
4603 ui_out_emit_tuple ui_emitter (m_uiout, nullptr);
4604
4605 /* Print it and reset *FIRST. */
4606 if (!m_first)
4607 m_uiout->text (", ");
4608 m_first = false;
4609
4610 m_uiout->wrap_hint (0);
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 }
4627}
4628
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
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
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
4682/* See symtab.h. */
4683
4684void
4685info_sources_worker (struct ui_out *uiout,
4686 bool group_by_objfile,
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");
4692 std::optional<ui_out_emit_tuple> output_tuple;
4693 std::optional<ui_out_emit_list> sources_list;
4694
4695 gdb_assert (group_by_objfile || uiout->is_mi_like_p ());
4696
4697 for (objfile *objfile : current_program_space->objfiles ())
4698 {
4699 if (group_by_objfile)
4700 {
4701 output_tuple.emplace (uiout, nullptr);
4702 uiout->field_string ("filename", objfile_name (objfile),
4703 file_name_style.style ());
4704 uiout->text (":\n");
4705 bool debug_fully_readin = !objfile->has_unexpanded_symtabs ();
4706 if (uiout->is_mi_like_p ())
4707 {
4708 const char *debug_info_state;
4709 if (objfile->has_symbols ())
4710 {
4711 if (debug_fully_readin)
4712 debug_info_state = "fully-read";
4713 else
4714 debug_info_state = "partially-read";
4715 }
4716 else
4717 debug_info_state = "none";
4718 current_uiout->field_string ("debug-info", debug_info_state);
4719 }
4720 else
4721 {
4722 if (!debug_fully_readin)
4723 uiout->text ("(Full debug information has not yet been read "
4724 "for this file.)\n");
4725 if (!objfile->has_symbols ())
4726 uiout->text ("(Objfile has no debug information.)\n");
4727 uiout->text ("\n");
4728 }
4729 sources_list.emplace (uiout, "sources");
4730 }
4731
4732 for (compunit_symtab *cu : objfile->compunits ())
4733 {
4734 for (symtab *s : cu->filetabs ())
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 }
4741
4742 if (group_by_objfile)
4743 {
4744 objfile->map_symbol_filenames (data, true /* need_fullname */);
4745 if (data.printed_filename_p ())
4746 uiout->text ("\n\n");
4747 data.reset_output ();
4748 sources_list.reset ();
4749 output_tuple.reset ();
4750 }
4751 }
4752
4753 if (!group_by_objfile)
4754 {
4755 data.reset_output ();
4756 map_symbol_filenames (data, true /*need_fullname*/);
4757 }
4758}
4759
4760/* Implement the 'info sources' command. */
4761
4762static void
4763info_sources_command (const char *args, int from_tty)
4764{
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."));
4768
4769 filename_partial_match_opts match_opts;
4770 auto group = make_info_sources_options_def_group (&match_opts);
4771 gdb::option::process_options
4772 (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
4773
4774 if (match_opts.dirname && match_opts.basename)
4775 error (_("You cannot give both -basename and -dirname to 'info sources'."));
4776
4777 const char *regex = nullptr;
4778 if (args != NULL && *args != '\000')
4779 regex = args;
4780
4781 if ((match_opts.dirname || match_opts.basename) && regex == nullptr)
4782 error (_("Missing REGEXP for 'info sources'."));
4783
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;
4789 else
4790 match_type = info_sources_filter::match_on::FULLNAME;
4791
4792 info_sources_filter filter (match_type, regex);
4793 info_sources_worker (current_uiout, true, filter);
4794}
4795
4796/* Compare FILE against all the entries of FILENAMES. If BASENAMES is
4797 true compare only lbasename of FILENAMES. */
4798
4799static bool
4800file_matches (const char *file,
4801 const std::vector<gdb::unique_xmalloc_ptr<char>> &filenames,
4802 bool basenames)
4803{
4804 if (filenames.empty ())
4805 return true;
4806
4807 for (const auto &name : filenames)
4808 {
4809 const char *lname = (basenames ? lbasename (name.get ()) : name.get ());
4810 if (compare_filenames_for_search (file, lname))
4811 return true;
4812 }
4813
4814 return false;
4815}
4816
4817/* Helper function for std::sort on symbol_search objects. Can only sort
4818 symbols, not minimal symbols. */
4819
4820int
4821symbol_search::compare_search_syms (const symbol_search &sym_a,
4822 const symbol_search &sym_b)
4823{
4824 int c;
4825
4826 c = FILENAME_CMP (sym_a.symbol->symtab ()->filename,
4827 sym_b.symbol->symtab ()->filename);
4828 if (c != 0)
4829 return c;
4830
4831 if (sym_a.block != sym_b.block)
4832 return sym_a.block - sym_b.block;
4833
4834 return strcmp (sym_a.symbol->print_name (), sym_b.symbol->print_name ());
4835}
4836
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
4847 symbol_lookup_debug_printf_v ("treg_matches_sym_type_name, sym %s",
4848 sym->natural_name ());
4849
4850 sym_type = sym->type ();
4851 if (sym_type == NULL)
4852 return false;
4853
4854 {
4855 scoped_switch_to_sym_language_if_auto l (sym);
4856
4857 printed_sym_type_name = type_to_string (sym_type);
4858 }
4859
4860 symbol_lookup_debug_printf_v ("sym_type_name %s",
4861 printed_sym_type_name.c_str ());
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
4869/* See symtab.h. */
4870
4871bool
4872global_symbol_searcher::is_suitable_msymbol
4873 (const domain_search_flags kind, const minimal_symbol *msymbol)
4874{
4875 switch (msymbol->type ())
4876 {
4877 case mst_data:
4878 case mst_bss:
4879 case mst_file_data:
4880 case mst_file_bss:
4881 return (kind & SEARCH_VAR_DOMAIN) != 0;
4882 case mst_text:
4883 case mst_file_text:
4884 case mst_solib_trampoline:
4885 case mst_text_gnu_ifunc:
4886 return (kind & SEARCH_FUNCTION_DOMAIN) != 0;
4887 default:
4888 return false;
4889 }
4890}
4891
4892/* See symtab.h. */
4893
4894bool
4895global_symbol_searcher::expand_symtabs
4896 (objfile *objfile, const std::optional<compiled_regex> &preg) const
4897{
4898 domain_search_flags kind = m_kind;
4899 bool found_msymbol = false;
4900
4901 auto do_file_match = [&] (const char *filename, bool basenames)
4902 {
4903 return file_matches (filename, m_filenames, basenames);
4904 };
4905 expand_symtabs_file_matcher file_matcher = nullptr;
4906 if (!m_filenames.empty ())
4907 file_matcher = do_file_match;
4908
4909 objfile->expand_symtabs_matching
4910 (file_matcher,
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,
4918 SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
4919 kind);
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. */
4935 if (m_filenames.empty ()
4936 && (kind & (SEARCH_VAR_DOMAIN | SEARCH_FUNCTION_DOMAIN)) != 0)
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. */
4956 if ((kind & SEARCH_FUNCTION_DOMAIN) != 0
4957 ? (find_pc_compunit_symtab
4958 (msymbol->value_address (objfile)) == NULL)
4959 : (lookup_symbol_in_objfile_from_linkage_name
4960 (objfile, msymbol->linkage_name (),
4961 SEARCH_VFT)
4962 .symbol == NULL))
4963 found_msymbol = true;
4964 }
4965 }
4966 }
4967 }
4968
4969 return found_msymbol;
4970}
4971
4972/* See symtab.h. */
4973
4974bool
4975global_symbol_searcher::add_matching_symbols
4976 (objfile *objfile,
4977 const std::optional<compiled_regex> &preg,
4978 const std::optional<compiled_regex> &treg,
4979 std::set<symbol_search> *result_set) const
4980{
4981 domain_search_flags kind = m_kind;
4982
4983 /* Add matching symbols (if not already present). */
4984 for (compunit_symtab *cust : objfile->compunits ())
4985 {
4986 const struct blockvector *bv = cust->blockvector ();
4987
4988 for (block_enum block : { GLOBAL_BLOCK, STATIC_BLOCK })
4989 {
4990 const struct block *b = bv->block (block);
4991
4992 for (struct symbol *sym : block_iterator_range (b))
4993 {
4994 struct symtab *real_symtab = sym->symtab ();
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. */
5001 if (!(file_matches (real_symtab->filename, m_filenames, false)
5002 || ((basenames_may_differ
5003 || file_matches (lbasename (real_symtab->filename),
5004 m_filenames, true))
5005 && file_matches (symtab_to_fullname (real_symtab),
5006 m_filenames, false))))
5007 continue;
5008
5009 if (!sym->matches (kind))
5010 continue;
5011
5012 if (preg.has_value () && preg->exec (sym->natural_name (), 0,
5013 nullptr, 0) != 0)
5014 continue;
5015
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)
5023 {
5024 if (sym->aclass () == LOC_UNRESOLVED
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. */
5029 || (sym->aclass () == LOC_CONST
5030 && (sym->type ()->code () == TYPE_CODE_ENUM)))
5031 continue;
5032 }
5033 if (sym->domain () == MODULE_DOMAIN && sym->line () == 0)
5034 continue;
5035
5036 if (result_set->size () < m_max_search_results)
5037 {
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);
5042 }
5043 else
5044 return false;
5045 }
5046 }
5047 }
5048
5049 return true;
5050}
5051
5052/* See symtab.h. */
5053
5054bool
5055global_symbol_searcher::add_matching_msymbols
5056 (objfile *objfile, const std::optional<compiled_regex> &preg,
5057 std::vector<symbol_search> *results) const
5058{
5059 domain_search_flags kind = m_kind;
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. */
5076 if ((kind & SEARCH_FUNCTION_DOMAIN) == 0
5077 || (find_pc_compunit_symtab
5078 (msymbol->value_address (objfile)) == NULL))
5079 {
5080 if (lookup_symbol_in_objfile_from_linkage_name
5081 (objfile, msymbol->linkage_name (),
5082 SEARCH_VFT).symbol == NULL)
5083 {
5084 /* Matching msymbol, add it to the results list. */
5085 if (results->size () < m_max_search_results)
5086 results->emplace_back (GLOBAL_BLOCK, msymbol, objfile);
5087 else
5088 return false;
5089 }
5090 }
5091 }
5092 }
5093 }
5094
5095 return true;
5096}
5097
5098/* See symtab.h. */
5099
5100std::vector<symbol_search>
5101global_symbol_searcher::search () const
5102{
5103 std::optional<compiled_regex> preg;
5104 std::optional<compiled_regex> treg;
5105
5106 if (m_symbol_name_regexp != NULL)
5107 {
5108 const char *symbol_name_regexp = m_symbol_name_regexp;
5109 std::string symbol_name_regexp_holder;
5110
5111 /* Make sure spacing is right for C++ operators.
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>. */
5115 const char *opend;
5116 const char *opname = operator_chars (symbol_name_regexp, &opend);
5117
5118 if (*opname)
5119 {
5120 int fix = -1; /* -1 means ok; otherwise number of
5121 spaces needed. */
5122
5123 if (isalpha (*opname) || *opname == '_' || *opname == '$')
5124 {
5125 /* There should 1 space between 'operator' and 'TYPENAME'. */
5126 if (opname[-1] != ' ' || opname[-2] == ' ')
5127 fix = 1;
5128 }
5129 else
5130 {
5131 /* There should 0 spaces between 'operator' and 'OPERATOR'. */
5132 if (opname[-1] == ' ')
5133 fix = 0;
5134 }
5135 /* If wrong number of spaces, fix it. */
5136 if (fix >= 0)
5137 {
5138 symbol_name_regexp_holder
5139 = string_printf ("operator%.*s%s", fix, " ", opname);
5140 symbol_name_regexp = symbol_name_regexp_holder.c_str ();
5141 }
5142 }
5143
5144 int cflags = REG_NOSUB | (case_sensitivity == case_sensitive_off
5145 ? REG_ICASE : 0);
5146 preg.emplace (symbol_name_regexp, cflags,
5147 _("Invalid regexp"));
5148 }
5149
5150 if (m_symbol_type_regexp != NULL)
5151 {
5152 int cflags = REG_NOSUB | (case_sensitivity == case_sensitive_off
5153 ? REG_ICASE : 0);
5154 treg.emplace (m_symbol_type_regexp, cflags,
5155 _("Invalid regexp"));
5156 }
5157
5158 bool found_msymbol = false;
5159 std::set<symbol_search> result_set;
5160 for (objfile *objfile : current_program_space->objfiles ())
5161 {
5162 /* Expand symtabs within objfile that possibly contain matching
5163 symbols. */
5164 found_msymbol |= expand_symtabs (objfile, preg);
5165
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;
5172 }
5173
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 ());
5177
5178 /* If there are no debug symbols, then add matching minsyms. But if the
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. */
5182 if ((found_msymbol
5183 || (m_filenames.empty () && (m_kind & SEARCH_VAR_DOMAIN) != 0))
5184 && !m_exclude_minsyms
5185 && !treg.has_value ())
5186 {
5187 gdb_assert ((m_kind & (SEARCH_VAR_DOMAIN | SEARCH_FUNCTION_DOMAIN))
5188 != 0);
5189 for (objfile *objfile : current_program_space->objfiles ())
5190 if (!add_matching_msymbols (objfile, preg, &result))
5191 break;
5192 }
5193
5194 return result;
5195}
5196
5197/* See symtab.h. */
5198
5199std::string
5200symbol_to_info_string (struct symbol *sym, int block)
5201{
5202 std::string str;
5203
5204 gdb_assert (block == GLOBAL_BLOCK || block == STATIC_BLOCK);
5205
5206 if (block == STATIC_BLOCK
5207 && (sym->domain () == VAR_DOMAIN
5208 || sym->domain () == FUNCTION_DOMAIN))
5209 str += "static ";
5210
5211 /* Typedef that is not a C++ class. */
5212 if (sym->domain () == TYPE_DOMAIN)
5213 {
5214 string_file tmp_stream;
5215
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. */
5224 if (sym->type ()->code () == TYPE_CODE_TYPEDEF)
5225 typedef_print (sym->type (), sym, &tmp_stream);
5226 else
5227 type_print (sym->type (), "", &tmp_stream, -1);
5228 str += tmp_stream.string ();
5229 }
5230 /* variable, func, or typedef-that-is-c++-class. */
5231 else if (sym->domain () == VAR_DOMAIN || sym->domain () == STRUCT_DOMAIN
5232 || sym->domain () == FUNCTION_DOMAIN)
5233 {
5234 string_file tmp_stream;
5235
5236 type_print (sym->type (),
5237 (sym->aclass () == LOC_TYPEDEF
5238 ? "" : sym->print_name ()),
5239 &tmp_stream, 0);
5240
5241 str += tmp_stream.string ();
5242 str += ";";
5243 }
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. */
5247 else if (sym->domain () == MODULE_DOMAIN)
5248 str += sym->print_name ();
5249
5250 return str;
5251}
5252
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. */
5259
5260static void
5261print_symbol_info (struct symbol *sym, int block, const char *last)
5262{
5263 scoped_switch_to_sym_language_if_auto l (sym);
5264 struct symtab *s = sym->symtab ();
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 {
5272 gdb_printf (_("\nFile %ps:\n"),
5273 styled_string (file_name_style.style (),
5274 s_filename));
5275 }
5276
5277 if (sym->line () != 0)
5278 gdb_printf ("%d:\t", sym->line ());
5279 else
5280 gdb_puts ("\t");
5281 }
5282
5283 std::string str = symbol_to_info_string (sym, block);
5284 gdb_printf ("%s\n", str.c_str ());
5285}
5286
5287/* This help function for symtab_symbol_info() prints information
5288 for non-debugging symbols to gdb_stdout. */
5289
5290static void
5291print_msymbol_info (bound_minimal_symbol msymbol)
5292{
5293 struct gdbarch *gdbarch = msymbol.objfile->arch ();
5294 const char *tmp;
5295
5296 if (gdbarch_addr_bit (gdbarch) <= 32)
5297 tmp = hex_string_custom (msymbol.value_address ()
5298 & (CORE_ADDR) 0xffffffff,
5299 8);
5300 else
5301 tmp = hex_string_custom (msymbol.value_address (),
5302 16);
5303
5304 ui_file_style sym_style = (msymbol.minsym->text_p ()
5305 ? function_name_style.style ()
5306 : ui_file_style ());
5307
5308 gdb_printf (_("%ps %ps\n"),
5309 styled_string (address_style.style (), tmp),
5310 styled_string (sym_style, msymbol.minsym->print_name ()));
5311}
5312
5313/* This is the guts of the commands "info functions", "info types", and
5314 "info variables". It calls search_symbols to find all matches and then
5315 print_[m]symbol_info to print out some useful information about the
5316 matches. */
5317
5318static void
5319symtab_symbol_info (bool quiet, bool exclude_minsyms,
5320 const char *regexp, domain_enum kind,
5321 const char *t_regexp, int from_tty)
5322{
5323 const char *last_filename = "";
5324 int first = 1;
5325
5326 if (regexp != nullptr && *regexp == '\0')
5327 regexp = nullptr;
5328
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);
5334 spec.set_symbol_type_regexp (t_regexp);
5335 spec.set_exclude_minsyms (exclude_minsyms);
5336 std::vector<symbol_search> symbols = spec.search ();
5337
5338 if (!quiet)
5339 {
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
5359 if (regexp != NULL)
5360 {
5361 if (t_regexp != NULL)
5362 gdb_printf
5363 (_("All %ss matching regular expression \"%s\""
5364 " with type matching regular expression \"%s\":\n"),
5365 classname, regexp, t_regexp);
5366 else
5367 gdb_printf (_("All %ss matching regular expression \"%s\":\n"),
5368 classname, regexp);
5369 }
5370 else
5371 {
5372 if (t_regexp != NULL)
5373 gdb_printf
5374 (_("All defined %ss"
5375 " with type matching regular expression \"%s\" :\n"),
5376 classname, t_regexp);
5377 else
5378 gdb_printf (_("All defined %ss:\n"), classname);
5379 }
5380 }
5381
5382 for (const symbol_search &p : symbols)
5383 {
5384 QUIT;
5385
5386 if (p.msymbol.minsym != NULL)
5387 {
5388 if (first)
5389 {
5390 if (!quiet)
5391 gdb_printf (_("\nNon-debugging symbols:\n"));
5392 first = 0;
5393 }
5394 print_msymbol_info (p.msymbol);
5395 }
5396 else
5397 {
5398 print_symbol_info (p.symbol, p.block, last_filename);
5399 last_filename
5400 = symtab_to_filename_for_display (p.symbol->symtab ());
5401 }
5402 }
5403}
5404
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
5409struct info_vars_funcs_options
5410{
5411 bool quiet = false;
5412 bool exclude_minsyms = false;
5413 std::string type_regexp;
5414};
5415
5416/* The options used by the 'info variables' and 'info functions'
5417 commands. */
5418
5419static const gdb::option::option_def info_vars_funcs_options_defs[] = {
5420 gdb::option::boolean_option_def<info_vars_funcs_options> {
5421 "q",
5422 [] (info_vars_funcs_options *opt) { return &opt->quiet; },
5423 nullptr, /* show_cmd_cb */
5424 nullptr /* set_doc */
5425 },
5426
5427 gdb::option::boolean_option_def<info_vars_funcs_options> {
5428 "n",
5429 [] (info_vars_funcs_options *opt) { return &opt->exclude_minsyms; },
5430 nullptr, /* show_cmd_cb */
5431 nullptr /* set_doc */
5432 },
5433
5434 gdb::option::string_option_def<info_vars_funcs_options> {
5435 "t",
5436 [] (info_vars_funcs_options *opt) { return &opt->type_regexp; },
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
5446make_info_vars_funcs_options_def_group (info_vars_funcs_options *opts)
5447{
5448 return {{info_vars_funcs_options_defs}, opts};
5449}
5450
5451/* Command completer for 'info variables' and 'info functions'. */
5452
5453static void
5454info_vars_funcs_command_completer (struct cmd_list_element *ignore,
5455 completion_tracker &tracker,
5456 const char *text, const char * /* word */)
5457{
5458 const auto group
5459 = make_info_vars_funcs_options_def_group (nullptr);
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
5468/* Implement the 'info variables' command. */
5469
5470static void
5471info_variables_command (const char *args, int from_tty)
5472{
5473 info_vars_funcs_options opts;
5474 auto grp = make_info_vars_funcs_options_def_group (&opts);
5475 gdb::option::process_options
5476 (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
5477 if (args != nullptr && *args == '\0')
5478 args = nullptr;
5479
5480 symtab_symbol_info
5481 (opts.quiet, opts.exclude_minsyms, args, VAR_DOMAIN,
5482 opts.type_regexp.empty () ? nullptr : opts.type_regexp.c_str (),
5483 from_tty);
5484}
5485
5486/* Implement the 'info functions' command. */
5487
5488static void
5489info_functions_command (const char *args, int from_tty)
5490{
5491 info_vars_funcs_options opts;
5492
5493 auto grp = make_info_vars_funcs_options_def_group (&opts);
5494 gdb::option::process_options
5495 (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
5496 if (args != nullptr && *args == '\0')
5497 args = nullptr;
5498
5499 symtab_symbol_info
5500 (opts.quiet, opts.exclude_minsyms, args, FUNCTION_DOMAIN,
5501 opts.type_regexp.empty () ? nullptr : opts.type_regexp.c_str (),
5502 from_tty);
5503}
5504
5505/* Holds the -q option for the 'info types' command. */
5506
5507struct info_types_options
5508{
5509 bool quiet = false;
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. */
5532
5533static void
5534info_types_command (const char *args, int from_tty)
5535{
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;
5543 symtab_symbol_info (opts.quiet, false, args, TYPE_DOMAIN, nullptr,
5544 from_tty);
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);
5562}
5563
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;
5576 symtab_symbol_info (opts.quiet, true, args, MODULE_DOMAIN, nullptr,
5577 from_tty);
5578}
5579
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
5588static void
5589rbreak_command (const char *regexp, int from_tty)
5590{
5591 gdb::unique_xmalloc_ptr<char> file_name;
5592
5593 if (regexp != nullptr)
5594 {
5595 const char *colon = strchr (regexp, ':');
5596
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
5602 if (colon && *(colon + 1) != ':')
5603 {
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);
5609 regexp = skip_spaces (colon + 1);
5610 }
5611 }
5612
5613 global_symbol_searcher spec (SEARCH_FUNCTION_DOMAIN, regexp);
5614 if (file_name != nullptr)
5615 spec.add_filename (std::move (file_name));
5616 std::vector<symbol_search> symbols = spec.search ();
5617
5618 gdb::unordered_set<std::string> seen_names;
5619 scoped_rbreak_breakpoints finalize;
5620 int err_count = 0;
5621
5622 for (const symbol_search &p : symbols)
5623 {
5624 std::string name;
5625 if (p.msymbol.minsym == nullptr)
5626 {
5627 if (file_name != nullptr)
5628 {
5629 struct symtab *symtab = p.symbol->symtab ();
5630 const char *fullname = symtab_to_fullname (symtab);
5631 name = string_printf ("%s:'%s'", fullname,
5632 p.symbol->linkage_name ());
5633 }
5634 else
5635 name = p.symbol->linkage_name ();
5636 }
5637 else
5638 name = p.msymbol.minsym->linkage_name ();
5639
5640 if (!seen_names.insert (name).second)
5641 continue;
5642
5643 try
5644 {
5645 break_command (name.c_str (), from_tty);
5646 }
5647 catch (const gdb_exception_error &ex)
5648 {
5649 exception_print (gdb_stderr, ex);
5650 ++err_count;
5651 continue;
5652 }
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 ());
5658 }
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);
5674}
5675\f
5676
5677/* Evaluate if SYMNAME matches LOOKUP_NAME. */
5678
5679static int
5680compare_symbol_name (const char *symbol_name, language symbol_language,
5681 const lookup_name_info &lookup_name,
5682 completion_match_result &match_res)
5683{
5684 const language_defn *lang = language_def (symbol_language);
5685
5686 symbol_name_matcher_ftype *name_match
5687 = lang->get_symbol_name_matcher (lookup_name);
5688
5689 return name_match (symbol_name, lookup_name, &match_res);
5690}
5691
5692/* See symtab.h. */
5693
5694bool
5695completion_list_add_name (completion_tracker &tracker,
5696 language symbol_language,
5697 const char *symname,
5698 const lookup_name_info &lookup_name,
5699 const char *text, const char *word)
5700{
5701 completion_match_result &match_res
5702 = tracker.reset_completion_match_result ();
5703
5704 /* Clip symbols that cannot match. */
5705 if (!compare_symbol_name (symname, symbol_language, lookup_name, match_res))
5706 return false;
5707
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
5714 /* We have a match for a completion, so add SYMNAME to the current list
5715 of matches. Note that the name is moved to freshly malloc'd space. */
5716
5717 {
5718 gdb::unique_xmalloc_ptr<char> completion
5719 = make_completion_match_str (symname, text, word);
5720
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),
5728 &match_res.match_for_lcd, text, word);
5729 }
5730
5731 return true;
5732}
5733
5734/* completion_list_add_name wrapper for struct symbol. */
5735
5736static void
5737completion_list_add_symbol (completion_tracker &tracker,
5738 symbol *sym,
5739 const lookup_name_info &lookup_name,
5740 const char *text, const char *word)
5741{
5742 if (!completion_list_add_name (tracker, sym->language (),
5743 sym->natural_name (),
5744 lookup_name, text, word))
5745 return;
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
5756 && sym->aclass () == LOC_BLOCK)
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. */
5761 gdb::unique_xmalloc_ptr<char> str
5762 = cp_canonicalize_string_no_typedefs (sym->natural_name ());
5763 if (str != nullptr)
5764 tracker.remove_completion (str.get ());
5765 }
5766}
5767
5768/* completion_list_add_name wrapper for struct minimal_symbol. */
5769
5770static void
5771completion_list_add_msymbol (completion_tracker &tracker,
5772 minimal_symbol *sym,
5773 const lookup_name_info &lookup_name,
5774 const char *text, const char *word)
5775{
5776 completion_list_add_name (tracker, sym->language (),
5777 sym->natural_name (),
5778 lookup_name, text, word);
5779}
5780
5781
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
5786completion_list_objc_symbol (completion_tracker &tracker,
5787 struct minimal_symbol *msymbol,
5788 const lookup_name_info &lookup_name,
5789 const char *text, const char *word)
5790{
5791 static char *tmp = NULL;
5792 static unsigned int tmplen = 0;
5793
5794 const char *method, *category, *selector;
5795 char *tmp2 = NULL;
5796
5797 method = msymbol->natural_name ();
5798
5799 /* Is it a method? */
5800 if ((method[0] != '-') && (method[0] != '+'))
5801 return;
5802
5803 if (text[0] == '[')
5804 /* Complete on shortened method method. */
5805 completion_list_add_name (tracker, language_objc,
5806 method + 1,
5807 lookup_name,
5808 text, word);
5809
5810 while ((strlen (method) + 1) >= tmplen)
5811 {
5812 if (tmplen == 0)
5813 tmplen = 1024;
5814 else
5815 tmplen *= 2;
5816 tmp = (char *) xrealloc (tmp, tmplen);
5817 }
5818 selector = strchr (method, ' ');
5819 if (selector != NULL)
5820 selector++;
5821
5822 category = strchr (method, '(');
5823
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);
5829 completion_list_add_name (tracker, language_objc, tmp,
5830 lookup_name, text, word);
5831 if (text[0] == '[')
5832 completion_list_add_name (tracker, language_objc, tmp + 1,
5833 lookup_name, text, word);
5834 }
5835
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';
5843
5844 completion_list_add_name (tracker, language_objc, tmp,
5845 lookup_name, text, word);
5846 }
5847}
5848
5849/* Break the non-quoted text based on the characters which are in
5850 symbols. FIXME: This should probably be language-specific. */
5851
5852static const char *
5853language_search_unquoted_string (const char *text, const char *p)
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 {
5863 if (p[-1] == ':') /* Might be part of a method name. */
5864 continue;
5865 else if (p[-1] == '[' && (p[-2] == '-' || p[-2] == '+'))
5866 p -= 2; /* Beginning of a method name. */
5867 else if (p[-1] == ' ' || p[-1] == '(' || p[-1] == ')')
5868 { /* Might be part of a method name. */
5869 const char *t = p;
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] == '+'))
5885 p = t - 2; /* Method name detected. */
5886 /* Else we leave with p unchanged. */
5887 }
5888 }
5889 break;
5890 }
5891 }
5892 return p;
5893}
5894
5895static void
5896completion_list_add_fields (completion_tracker &tracker,
5897 struct symbol *sym,
5898 const lookup_name_info &lookup_name,
5899 const char *text, const char *word)
5900{
5901 if (sym->aclass () == LOC_TYPEDEF)
5902 {
5903 struct type *t = sym->type ();
5904 enum type_code c = t->code ();
5905 int j;
5906
5907 if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
5908 for (j = TYPE_N_BASECLASSES (t); j < t->num_fields (); j++)
5909 if (t->field (j).name ())
5910 completion_list_add_name (tracker, sym->language (),
5911 t->field (j).name (),
5912 lookup_name, text, word);
5913 }
5914}
5915
5916/* See symtab.h. */
5917
5918bool
5919symbol_is_function_or_method (symbol *sym)
5920{
5921 switch (sym->type ()->code ())
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{
5936 switch (msymbol->type ())
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
5948/* See symtab.h. */
5949
5950bound_minimal_symbol
5951find_gnu_ifunc (const symbol *sym)
5952{
5953 if (sym->aclass () != LOC_BLOCK)
5954 return {};
5955
5956 lookup_name_info lookup_name (sym->search_name (),
5957 symbol_name_match_type::SEARCH_NAME);
5958 struct objfile *objfile = sym->objfile ();
5959
5960 CORE_ADDR address = sym->value_block ()->entry_pc ();
5961 minimal_symbol *ifunc = NULL;
5962
5963 iterate_over_minimal_symbols (objfile, lookup_name,
5964 [&] (minimal_symbol *minsym)
5965 {
5966 if (minsym->type () == mst_text_gnu_ifunc
5967 || minsym->type () == mst_data_gnu_ifunc)
5968 {
5969 CORE_ADDR msym_addr = minsym->value_address (objfile);
5970 if (minsym->type () == mst_data_gnu_ifunc)
5971 {
5972 struct gdbarch *gdbarch = objfile->arch ();
5973 msym_addr = gdbarch_convert_from_func_ptr_addr
5974 (gdbarch, msym_addr, current_inferior ()->top_target ());
5975 }
5976 if (msym_addr == address)
5977 {
5978 ifunc = minsym;
5979 return true;
5980 }
5981 }
5982 return false;
5983 });
5984
5985 if (ifunc != NULL)
5986 return {ifunc, objfile};
5987 return {};
5988}
5989
5990/* Add matching symbols from SYMTAB to the current completion list. */
5991
5992static void
5993add_symtab_completions (struct compunit_symtab *cust,
5994 completion_tracker &tracker,
5995 complete_symbol_mode mode,
5996 const lookup_name_info &lookup_name,
5997 const char *text, const char *word,
5998 enum type_code code)
5999{
6000 int i;
6001
6002 if (cust == NULL)
6003 return;
6004
6005 for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
6006 {
6007 QUIT;
6008
6009 const struct block *b = cust->blockvector ()->block (i);
6010 for (struct symbol *sym : block_iterator_range (b))
6011 {
6012 if (completion_skip_symbol (mode, sym))
6013 continue;
6014
6015 if (code == TYPE_CODE_UNDEF
6016 || (sym->domain () == STRUCT_DOMAIN
6017 && sym->type ()->code () == code))
6018 completion_list_add_symbol (tracker, sym,
6019 lookup_name,
6020 text, word);
6021 }
6022 }
6023}
6024
6025void
6026default_collect_symbol_completion_matches_break_on
6027 (completion_tracker &tracker, complete_symbol_mode mode,
6028 symbol_name_match_type name_match_type,
6029 const char *text, const char *word,
6030 const char *break_on, enum type_code code)
6031{
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
6036 const struct block *b;
6037 const struct block *surrounding_static_block, *surrounding_global_block;
6038 /* The symbol we are completing on. Points in same buffer as text. */
6039 const char *sym_text;
6040
6041 /* Now look for the symbol we are supposed to complete on. */
6042 if (mode == complete_symbol_mode::LINESPEC)
6043 sym_text = text;
6044 else
6045 {
6046 const char *p;
6047 char quote_found;
6048 const char *quote_pos = NULL;
6049
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 }
6094
6095 lookup_name_info lookup_name (sym_text, name_match_type, true);
6096
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
6100 handled by the psymtab code below). */
6101
6102 if (code == TYPE_CODE_UNDEF)
6103 {
6104 for (objfile *objfile : current_program_space->objfiles ())
6105 {
6106 for (minimal_symbol *msymbol : objfile->msymbols ())
6107 {
6108 QUIT;
6109
6110 if (completion_skip_symbol (mode, msymbol))
6111 continue;
6112
6113 completion_list_add_msymbol (tracker, msymbol, lookup_name,
6114 sym_text, word);
6115
6116 completion_list_objc_symbol (tracker, msymbol, lookup_name,
6117 sym_text, word);
6118 }
6119 }
6120 }
6121
6122 /* Add completions for all currently loaded symbol tables. */
6123 for (objfile *objfile : current_program_space->objfiles ())
6124 {
6125 for (compunit_symtab *cust : objfile->compunits ())
6126 add_symtab_completions (cust, tracker, mode, lookup_name,
6127 sym_text, word, code);
6128 }
6129
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,
6133 lookup_name,
6134 NULL,
6135 [&] (compunit_symtab *symtab) /* expansion notify */
6136 {
6137 add_symtab_completions (symtab,
6138 tracker, mode, lookup_name,
6139 sym_text, word, code);
6140 return true;
6141 },
6142 SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
6143 SEARCH_ALL_DOMAINS);
6144
6145 /* Search upwards from currently selected frame (so that we can
6146 complete on local vars). Also catch fields of types defined in
6147 this places which match our text string. Only complete on types
6148 visible from current context. */
6149
6150 b = get_selected_block (0);
6151 surrounding_static_block = b == nullptr ? nullptr : b->static_block ();
6152 surrounding_global_block = b == nullptr ? nullptr : b->global_block ();
6153 if (surrounding_static_block != NULL)
6154 while (b != surrounding_static_block)
6155 {
6156 QUIT;
6157
6158 for (struct symbol *sym : block_iterator_range (b))
6159 {
6160 if (code == TYPE_CODE_UNDEF)
6161 {
6162 completion_list_add_symbol (tracker, sym, lookup_name,
6163 sym_text, word);
6164 completion_list_add_fields (tracker, sym, lookup_name,
6165 sym_text, word);
6166 }
6167 else if (sym->domain () == STRUCT_DOMAIN
6168 && sym->type ()->code () == code)
6169 completion_list_add_symbol (tracker, sym, lookup_name,
6170 sym_text, word);
6171 }
6172
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. */
6176 if (b->function () != NULL && b->inlined_p ())
6177 break;
6178 b = b->superblock ();
6179 }
6180
6181 /* Add fields from the file's types; symbols will be added below. */
6182
6183 if (code == TYPE_CODE_UNDEF)
6184 {
6185 if (surrounding_static_block != NULL)
6186 for (struct symbol *sym : block_iterator_range (surrounding_static_block))
6187 completion_list_add_fields (tracker, sym, lookup_name,
6188 sym_text, word);
6189
6190 if (surrounding_global_block != NULL)
6191 for (struct symbol *sym : block_iterator_range (surrounding_global_block))
6192 completion_list_add_fields (tracker, sym, lookup_name,
6193 sym_text, word);
6194 }
6195
6196 /* Skip macros if we are completing a struct tag -- arguable but
6197 usually what is expected. */
6198 if (current_language->macro_expansion () == macro_expansion_c
6199 && code == TYPE_CODE_UNDEF)
6200 {
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 {
6207 completion_list_add_name (tracker, language_c, macro_name,
6208 lookup_name, sym_text, word);
6209 };
6210
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. */
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);
6221
6222 /* User-defined macros are always visible. */
6223 macro_for_each (macro_user_macros, add_macro_name);
6224 }
6225}
6226
6227/* Collect all symbols (regardless of class) which begin by matching
6228 TEXT. */
6229
6230void
6231collect_symbol_completion_matches (completion_tracker &tracker,
6232 complete_symbol_mode mode,
6233 symbol_name_match_type name_match_type,
6234 const char *text, const char *word)
6235{
6236 current_language->collect_symbol_completion_matches (tracker, mode,
6237 name_match_type,
6238 text, word,
6239 TYPE_CODE_UNDEF);
6240}
6241
6242/* Like collect_symbol_completion_matches, but only collect
6243 STRUCT_DOMAIN symbols whose type code is CODE. */
6244
6245void
6246collect_symbol_completion_matches_type (completion_tracker &tracker,
6247 const char *text, const char *word,
6248 enum type_code code)
6249{
6250 complete_symbol_mode mode = complete_symbol_mode::EXPRESSION;
6251 symbol_name_match_type name_match_type = symbol_name_match_type::EXPRESSION;
6252
6253 gdb_assert (code == TYPE_CODE_UNION
6254 || code == TYPE_CODE_STRUCT
6255 || code == TYPE_CODE_ENUM);
6256 current_language->collect_symbol_completion_matches (tracker, mode,
6257 name_match_type,
6258 text, word, code);
6259}
6260
6261/* Like collect_symbol_completion_matches, but collects a list of
6262 symbols defined in all source files named SRCFILE. */
6263
6264void
6265collect_file_symbol_completion_matches (completion_tracker &tracker,
6266 complete_symbol_mode mode,
6267 symbol_name_match_type name_match_type,
6268 const char *text, const char *word,
6269 const char *srcfile)
6270{
6271 /* The symbol we are completing on. Points in same buffer as text. */
6272 const char *sym_text;
6273
6274 /* Now look for the symbol we are supposed to complete on.
6275 FIXME: This should be language-specific. */
6276 if (mode == complete_symbol_mode::LINESPEC)
6277 sym_text = text;
6278 else
6279 {
6280 const char *p;
6281 char quote_found;
6282 const char *quote_pos = NULL;
6283
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 }
6319
6320 lookup_name_info lookup_name (sym_text, name_match_type, true);
6321
6322 /* Go through symtabs for SRCFILE and check the externs and statics
6323 for symbols which match. */
6324 iterate_over_symtabs (current_program_space, srcfile, [&] (symtab *s)
6325 {
6326 add_symtab_completions (s->compunit (),
6327 tracker, mode, lookup_name,
6328 sym_text, word, TYPE_CODE_UNDEF);
6329 return false;
6330 });
6331}
6332
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
6338add_filename_to_list (const char *fname, const char *text, const char *word,
6339 completion_list *list)
6340{
6341 list->emplace_back (make_completion_match_str (fname, text, word));
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 {
6355 if (filename_cmp (fname, illegal_aliens[i]) == 0)
6356 return 1;
6357 }
6358 return 0;
6359}
6360
6361/* An object of this type is passed as the callback argument to
6362 map_partial_symbol_filenames. */
6363struct add_partial_filename_data
6364{
6365 struct filename_seen_cache *filename_seen_cache;
6366 const char *text;
6367 const char *word;
6368 int text_len;
6369 completion_list *list;
6370
6371 void operator() (const char *filename, const char *fullname);
6372};
6373
6374/* A callback for map_partial_symbol_filenames. */
6375
6376void
6377add_partial_filename_data::operator() (const char *filename,
6378 const char *fullname)
6379{
6380 if (not_interesting_fname (filename))
6381 return;
6382 if (!filename_seen_cache->seen (filename)
6383 && filename_ncmp (filename, text, text_len) == 0)
6384 {
6385 /* This file matches for a completion; add it to the
6386 current list of matches. */
6387 add_filename_to_list (filename, text, word, list);
6388 }
6389 else
6390 {
6391 const char *base_name = lbasename (filename);
6392
6393 if (base_name != filename
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);
6397 }
6398}
6399
6400/* Return a list of all source files whose names begin with matching
6401 TEXT. The file names are looked up in the symbol tables of this
6402 program. */
6403
6404completion_list
6405make_source_files_completion_list (const char *text, const char *word)
6406{
6407 size_t text_len = strlen (text);
6408 completion_list list;
6409 const char *base_name;
6410 struct add_partial_filename_data datum;
6411
6412 if (!have_full_symbols (current_program_space)
6413 && !have_partial_symbols (current_program_space))
6414 return list;
6415
6416 filename_seen_cache filenames_seen;
6417
6418 for (objfile *objfile : current_program_space->objfiles ())
6419 {
6420 for (compunit_symtab *cu : objfile->compunits ())
6421 {
6422 for (symtab *s : cu->filetabs ())
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 }
6446 }
6447 }
6448
6449 datum.filename_seen_cache = &filenames_seen;
6450 datum.text = text;
6451 datum.word = word;
6452 datum.text_len = text_len;
6453 datum.list = &list;
6454 map_symbol_filenames (datum, false /*need_fullname*/);
6455
6456 return list;
6457}
6458\f
6459/* Track MAIN */
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
6465static main_info *
6466get_main_info (program_space *pspace)
6467{
6468 main_info *info = main_progspace_key.get (pspace);
6469
6470 if (info == NULL)
6471 {
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. */
6478 info = main_progspace_key.emplace (pspace);
6479 }
6480
6481 return info;
6482}
6483
6484static void
6485set_main_name (program_space *pspace, const char *name, enum language lang)
6486{
6487 main_info *info = get_main_info (pspace);
6488
6489 if (!info->name_of_main.empty ())
6490 {
6491 info->name_of_main.clear ();
6492 info->language_of_main = language_unknown;
6493 }
6494 if (name != NULL)
6495 {
6496 info->name_of_main = name;
6497 info->language_of_main = lang;
6498 }
6499}
6500
6501/* Deduce the name of the main procedure, and set NAME_OF_MAIN
6502 accordingly. */
6503
6504static void
6505find_main_name (void)
6506{
6507 const char *new_main_name;
6508 program_space *pspace = current_program_space;
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. */
6516 for (objfile *objfile : current_program_space->objfiles ())
6517 {
6518 objfile->compute_main_name ();
6519
6520 if (objfile->per_bfd->name_of_main != NULL)
6521 {
6522 set_main_name (pspace,
6523 objfile->per_bfd->name_of_main,
6524 objfile->per_bfd->language_of_main);
6525 return;
6526 }
6527 }
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
6536 such that false positives never be returned. For instance, it is
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
6541 of the main procedure. It is difficult however to see how this can
6542 be guaranteed for languages such as C, for instance. This suggests
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)
6547 {
6548 set_main_name (pspace, new_main_name, language_ada);
6549 return;
6550 }
6551
6552 new_main_name = d_main_name ();
6553 if (new_main_name != NULL)
6554 {
6555 set_main_name (pspace, new_main_name, language_d);
6556 return;
6557 }
6558
6559 new_main_name = go_main_name ();
6560 if (new_main_name != NULL)
6561 {
6562 set_main_name (pspace, new_main_name, language_go);
6563 return;
6564 }
6565
6566 new_main_name = pascal_main_name ();
6567 if (new_main_name != NULL)
6568 {
6569 set_main_name (pspace, new_main_name, language_pascal);
6570 return;
6571 }
6572
6573 /* The languages above didn't identify the name of the main procedure.
6574 Fallback to "main". */
6575
6576 /* Try to find language for main in psymtabs. */
6577 bool symbol_found_p = false;
6578 gdbarch_iterate_over_objfiles_in_search_order
6579 (current_inferior ()->arch (),
6580 [&symbol_found_p, pspace] (objfile *obj)
6581 {
6582 language lang
6583 = obj->lookup_global_symbol_language ("main",
6584 SEARCH_FUNCTION_DOMAIN,
6585 &symbol_found_p);
6586 if (symbol_found_p)
6587 {
6588 set_main_name (pspace, "main", lang);
6589 return 1;
6590 }
6591
6592 return 0;
6593 }, nullptr);
6594
6595 if (symbol_found_p)
6596 return;
6597
6598 set_main_name (pspace, "main", language_unknown);
6599}
6600
6601/* See symtab.h. */
6602
6603const char *
6604main_name ()
6605{
6606 main_info *info = get_main_info (current_program_space);
6607
6608 if (info->name_of_main.empty ())
6609 find_main_name ();
6610
6611 return info->name_of_main.c_str ();
6612}
6613
6614/* Return the language of the main function. If it is not known,
6615 return language_unknown. */
6616
6617enum language
6618main_language (void)
6619{
6620 main_info *info = get_main_info (current_program_space);
6621
6622 if (info->name_of_main.empty ())
6623 find_main_name ();
6624
6625 return info->language_of_main;
6626}
6627
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. */
6636#define MAX_SYMBOL_IMPLS (LOC_FINAL_VALUE + 11)
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
6647gdb::array_view<const struct symbol_impl> symbol_impls (symbol_impl);
6648
6649/* Make sure we saved enough room in struct symbol. */
6650
6651static_assert (MAX_SYMBOL_IMPLS <= (1 << SYMBOL_ACLASS_BITS));
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
6669 /* Sanity check OPS. */
6670 gdb_assert (ops != NULL);
6671 gdb_assert (ops->tracepoint_var_ref != NULL);
6672 gdb_assert (ops->describe_location != NULL);
6673 gdb_assert (ops->get_symbol_read_needs != NULL);
6674 gdb_assert (ops->read_variable != NULL);
6675
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);
6697 gdb_assert (ops->find_frame_base_location != nullptr
6698 || ops->get_block_value != nullptr);
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)
6731 symbol_impl[i].aclass = (enum address_class) i;
6732}
6733
6734\f
6735
6736/* See symtab.h. */
6737
6738struct objfile *
6739symbol::objfile () const
6740{
6741 gdb_assert (is_objfile_owned ());
6742 return owner.symtab->compunit ()->objfile ();
6743}
6744
6745/* See symtab.h. */
6746
6747struct gdbarch *
6748symbol::arch () const
6749{
6750 if (!is_objfile_owned ())
6751 return owner.arch;
6752 return owner.symtab->compunit ()->objfile ()->arch ();
6753}
6754
6755/* See symtab.h. */
6756
6757struct symtab *
6758symbol::symtab () const
6759{
6760 gdb_assert (is_objfile_owned ());
6761 return owner.symtab;
6762}
6763
6764/* See symtab.h. */
6765
6766void
6767symbol::set_symtab (struct symtab *symtab)
6768{
6769 gdb_assert (is_objfile_owned ());
6770 owner.symtab = symtab;
6771}
6772
6773/* See symtab.h. */
6774
6775CORE_ADDR
6776symbol::get_maybe_copied_address () const
6777{
6778 gdb_assert (this->maybe_copied);
6779 gdb_assert (this->aclass () == LOC_STATIC);
6780
6781 const char *linkage_name = this->linkage_name ();
6782 bound_minimal_symbol minsym
6783 = lookup_minimal_symbol_linkage (this->objfile ()->pspace (), linkage_name,
6784 false, false);
6785 if (minsym.minsym != nullptr)
6786 return minsym.value_address ();
6787
6788 return this->m_value.address;
6789}
6790
6791/* See symtab.h. */
6792
6793CORE_ADDR
6794minimal_symbol::get_maybe_copied_address (objfile *objf) const
6795{
6796 gdb_assert (this->maybe_copied (objf));
6797 gdb_assert ((objf->flags & OBJF_MAINLINE) == 0);
6798
6799 const char *linkage_name = this->linkage_name ();
6800 bound_minimal_symbol found
6801 = lookup_minimal_symbol_linkage (objf->pspace (), linkage_name,
6802 false, true);
6803 if (found.minsym != nullptr)
6804 return found.value_address ();
6805
6806 return (this->m_value.address
6807 + objf->section_offsets[this->section_index ()]);
6808}
6809
6810\f
6811
6812/* Hold the sub-commands of 'info module'. */
6813
6814static struct cmd_list_element *info_module_cmdlist = NULL;
6815
6816/* See symtab.h. */
6817
6818std::vector<module_symbol_search>
6819search_module_symbols (const char *module_regexp, const char *regexp,
6820 const char *type_regexp, domain_search_flags kind)
6821{
6822 std::vector<module_symbol_search> results;
6823
6824 /* Search for all modules matching MODULE_REGEXP. */
6825 global_symbol_searcher spec1 (SEARCH_MODULE_DOMAIN, module_regexp);
6826 spec1.set_exclude_minsyms (true);
6827 std::vector<symbol_search> modules = spec1.search ();
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. */
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 ();
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
6846 std::string prefix = p.symbol->print_name ();
6847 prefix += "::";
6848
6849 for (const symbol_search &q : symbols)
6850 {
6851 if (q.symbol == nullptr)
6852 continue;
6853
6854 if (strncmp (q.symbol->print_name (), prefix.c_str (),
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,
6871 domain_search_flags kind)
6872{
6873 gdb_assert (kind == SEARCH_FUNCTION_DOMAIN || kind == SEARCH_VAR_DOMAIN);
6874
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)
6884 gdb_printf ((kind == SEARCH_VAR_DOMAIN
6885 ? _("All variables in all modules:")
6886 : _("All functions in all modules:")));
6887 else
6888 gdb_printf
6889 ((kind == SEARCH_VAR_DOMAIN
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)
6899 gdb_printf
6900 ((kind == SEARCH_VAR_DOMAIN
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
6907 gdb_printf
6908 ((kind == SEARCH_VAR_DOMAIN
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)
6923 gdb_printf
6924 ((kind == SEARCH_VAR_DOMAIN
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
6931 gdb_printf
6932 ((kind == SEARCH_VAR_DOMAIN
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)
6944 gdb_printf
6945 ((kind == SEARCH_VAR_DOMAIN
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
6954 gdb_printf
6955 ((kind == SEARCH_VAR_DOMAIN
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 }
6967 gdb_printf ("\n");
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 {
6998 gdb_printf ("\n");
6999 gdb_printf (_("Module \"%s\":\n"), p.symbol->print_name ());
7000 last_module_symbol = p.symbol;
7001 last_filename = "";
7002 }
7003
7004 print_symbol_info (q.symbol, q.block, last_filename);
7005 last_filename
7006 = symtab_to_filename_for_display (q.symbol->symtab ());
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;
7015 std::string type_regexp;
7016 std::string module_regexp;
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
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 (),
7070 SEARCH_FUNCTION_DOMAIN);
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
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 (),
7089 SEARCH_VAR_DOMAIN);
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
7112INIT_GDB_FILE (symtab)
7113{
7114 cmd_list_element *c;
7115
7116 initialize_ordinary_address_classes ();
7117
7118 c = add_info ("variables", info_variables_command,
7119 info_print_args_help (_("\
7120All global and static variable names or those matching REGEXPs.\n\
7121Usage: info variables [-q] [-n] [-t TYPEREGEXP] [NAMEREGEXP]\n\
7122Prints the global and static variables.\n"),
7123 _("global and static variables"),
7124 true));
7125 set_cmd_completer_handle_brkchars (c, info_vars_funcs_command_completer);
7126
7127 c = add_info ("functions", info_functions_command,
7128 info_print_args_help (_("\
7129All function names or those matching REGEXPs.\n\
7130Usage: info functions [-q] [-n] [-t TYPEREGEXP] [NAMEREGEXP]\n\
7131Prints the functions.\n"),
7132 _("functions"),
7133 true));
7134 set_cmd_completer_handle_brkchars (c, info_vars_funcs_command_completer);
7135
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);
7142
7143 const auto info_sources_opts
7144 = make_info_sources_options_def_group (nullptr);
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);
7158
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
7163 add_info ("main", info_main_command,
7164 _("Get main symbol to identify entry point into program."));
7165
7166 add_basic_prefix_cmd ("module", class_info, _("\
7167Print information about modules."),
7168 &info_module_cmdlist, 0, &infolist);
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
7204 add_com ("rbreak", class_breakpoint, rbreak_command,
7205 _("Set a breakpoint for all functions matching REGEXP."));
7206
7207 add_setshow_enum_cmd ("multiple-symbols", no_class,
7208 multiple_symbols_modes, &multiple_symbols_mode,
7209 _("\
7210Set how the debugger handles ambiguities in expressions."), _("\
7211Show how the debugger handles ambiguities in expressions."), _("\
7212Valid values are \"ask\", \"all\", \"cancel\", and the default is \"all\"."),
7213 NULL, NULL, &setlist, &showlist);
7214
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
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);
7238
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
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
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 _("\
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\
7264the use of prologue scanners."),
7265 nullptr, nullptr,
7266 &maintenance_set_cmdlist,
7267 &maintenance_show_cmdlist);
7268
7269
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
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,
7285 class_maintenance, 0, &maintenancelist);
7286 deprecate_cmd (c, "maintenance flush symbol-cache");
7287
7288 gdb::observers::new_objfile.attach (symtab_new_objfile_observer, "symtab");
7289 gdb::observers::all_objfiles_removed.attach (symtab_all_objfiles_removed,
7290 "symtab");
7291 gdb::observers::free_objfile.attach (symtab_free_objfile_observer, "symtab");
7292}