1 /* Symbol table lookup for the GNU debugger, GDB.
3 Copyright (C) 1986-2025 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "dwarf2/call-site.h"
21 #include "exceptions.h"
23 #include "event-top.h"
31 #include "gdbsupport/gdb_regex.h"
32 #include "expression.h"
37 #include "filenames.h"
38 #include "objc-lang.h"
44 #include "cli/cli-utils.h"
45 #include "cli/cli-style.h"
46 #include "cli/cli-cmds.h"
49 #include "typeprint.h"
50 #include "exceptions.h"
52 #include "gdbsupport/gdb_obstack.h"
54 #include "dictionary.h"
56 #include <sys/types.h>
60 #include "cp-support.h"
61 #include "observable.h"
63 #include "macroscope.h"
65 #include "parser-defs.h"
66 #include "completer.h"
67 #include "progspace-and-thread.h"
69 #include "filename-seen-cache.h"
70 #include "arch-utils.h"
72 #include <string_view>
73 #include "gdbsupport/pathstuff.h"
74 #include "gdbsupport/common-utils.h"
76 #include "gdbsupport/unordered_set.h"
78 /* Forward declarations for local functions. */
80 static void rbreak_command (const char *, int);
82 static int find_line_common (const linetable
*, int, int *, int);
84 static struct block_symbol
85 lookup_symbol_aux (const char *name
,
86 symbol_name_match_type match_type
,
87 const struct block
*block
,
88 const domain_search_flags domain
,
89 enum language language
,
90 struct field_of_this_result
*);
93 struct block_symbol
lookup_local_symbol (const char *name
,
94 symbol_name_match_type match_type
,
95 const struct block
*block
,
96 const domain_search_flags domain
,
97 const struct language_defn
*langdef
);
99 static struct block_symbol
100 lookup_symbol_in_objfile (struct objfile
*objfile
,
101 enum block_enum block_index
,
103 const domain_search_flags domain
);
105 static void set_main_name (program_space
*pspace
, const char *name
,
108 /* Type of the data stored on the program space. */
112 /* Name of "main". */
114 std::string name_of_main
;
116 /* Language of "main". */
118 enum language language_of_main
= language_unknown
;
121 /* Program space key for finding name and language of "main". */
123 static const registry
<program_space
>::key
<main_info
> main_progspace_key
;
125 /* The default symbol cache size.
126 There is no extra cpu cost for large N (except when flushing the cache,
127 which is rare). The value here is just a first attempt. A better default
128 value may be higher or lower. A prime number can make up for a bad hash
129 computation, so that's why the number is what it is. */
130 #define DEFAULT_SYMBOL_CACHE_SIZE 1021
132 /* The maximum symbol cache size.
133 There's no method to the decision of what value to use here, other than
134 there's no point in allowing a user typo to make gdb consume all memory. */
135 #define MAX_SYMBOL_CACHE_SIZE (1024*1024)
137 /* symbol_cache_lookup returns this if a previous lookup failed to find the
138 symbol in any objfile. */
139 #define SYMBOL_LOOKUP_FAILED \
140 ((struct block_symbol) {(struct symbol *) 1, NULL})
141 #define SYMBOL_LOOKUP_FAILED_P(SIB) (SIB.symbol == (struct symbol *) 1)
143 /* Recording lookups that don't find the symbol is just as important, if not
144 more so, than recording found symbols. */
146 enum symbol_cache_slot_state
149 SYMBOL_SLOT_NOT_FOUND
,
153 struct symbol_cache_slot
155 enum symbol_cache_slot_state state
;
157 /* The objfile that was current when the symbol was looked up.
158 This is only needed for global blocks, but for simplicity's sake
159 we allocate the space for both. If data shows the extra space used
160 for static blocks is a problem, we can split things up then.
162 Global blocks need cache lookup to include the objfile context because
163 we need to account for gdbarch_iterate_over_objfiles_in_search_order
164 which can traverse objfiles in, effectively, any order, depending on
165 the current objfile, thus affecting which symbol is found. Normally,
166 only the current objfile is searched first, and then the rest are
167 searched in recorded order; but putting cache lookup inside
168 gdbarch_iterate_over_objfiles_in_search_order would be awkward.
169 Instead we just make the current objfile part of the context of
170 cache lookup. This means we can record the same symbol multiple times,
171 each with a different "current objfile" that was in effect when the
172 lookup was saved in the cache, but cache space is pretty cheap. */
173 const struct objfile
*objfile_context
;
175 /* The domain that was searched for initially. This must exactly
177 domain_search_flags domain
;
181 struct block_symbol found
;
186 /* Clear out SLOT. */
189 symbol_cache_clear_slot (struct symbol_cache_slot
*slot
)
191 if (slot
->state
== SYMBOL_SLOT_NOT_FOUND
)
192 xfree (slot
->value
.name
);
193 slot
->state
= SYMBOL_SLOT_UNUSED
;
196 /* Symbols don't specify global vs static block.
197 So keep them in separate caches. */
199 struct block_symbol_cache
203 unsigned int collisions
;
205 /* SYMBOLS is a variable length array of this size.
206 One can imagine that in general one cache (global/static) should be a
207 fraction of the size of the other, but there's no data at the moment
208 on which to decide. */
211 struct symbol_cache_slot symbols
[1];
214 /* Clear all slots of BSC and free BSC. */
217 destroy_block_symbol_cache (struct block_symbol_cache
*bsc
)
221 for (unsigned int i
= 0; i
< bsc
->size
; i
++)
222 symbol_cache_clear_slot (&bsc
->symbols
[i
]);
229 Searching for symbols in the static and global blocks over multiple objfiles
230 again and again can be slow, as can searching very big objfiles. This is a
231 simple cache to improve symbol lookup performance, which is critical to
232 overall gdb performance.
234 Symbols are hashed on the name, its domain, and block.
235 They are also hashed on their objfile for objfile-specific lookups. */
239 symbol_cache () = default;
243 destroy_block_symbol_cache (global_symbols
);
244 destroy_block_symbol_cache (static_symbols
);
247 struct block_symbol_cache
*global_symbols
= nullptr;
248 struct block_symbol_cache
*static_symbols
= nullptr;
251 /* Program space key for finding its symbol cache. */
253 static const registry
<program_space
>::key
<symbol_cache
> symbol_cache_key
;
255 /* When non-zero, print debugging messages related to symtab creation. */
256 unsigned int symtab_create_debug
= 0;
258 /* When non-zero, print debugging messages related to symbol lookup. */
259 unsigned int symbol_lookup_debug
= 0;
261 /* The size of the cache is staged here. */
262 static unsigned int new_symbol_cache_size
= DEFAULT_SYMBOL_CACHE_SIZE
;
264 /* The current value of the symbol cache size.
265 This is saved so that if the user enters a value too big we can restore
266 the original value from here. */
267 static unsigned int symbol_cache_size
= DEFAULT_SYMBOL_CACHE_SIZE
;
269 /* True if a file may be known by two different basenames.
270 This is the uncommon case, and significantly slows down gdb.
271 Default set to "off" to not slow down the common case. */
272 bool basenames_may_differ
= false;
274 /* Allow the user to configure the debugger behavior with respect
275 to multiple-choice menus when more than one symbol matches during
278 const char multiple_symbols_ask
[] = "ask";
279 const char multiple_symbols_all
[] = "all";
280 const char multiple_symbols_cancel
[] = "cancel";
281 static const char *const multiple_symbols_modes
[] =
283 multiple_symbols_ask
,
284 multiple_symbols_all
,
285 multiple_symbols_cancel
,
288 static const char *multiple_symbols_mode
= multiple_symbols_all
;
290 /* When TRUE, ignore the prologue-end flag in linetable_entry when searching
291 for the SAL past a function prologue. */
292 static bool ignore_prologue_end_flag
= false;
294 /* Read-only accessor to AUTO_SELECT_MODE. */
297 multiple_symbols_select_mode (void)
299 return multiple_symbols_mode
;
302 /* Return the name of a domain_enum. */
305 domain_name (domain_enum e
)
309 #define SYM_DOMAIN(X) \
310 case X ## _DOMAIN: return #X "_DOMAIN";
311 #include "sym-domains.def"
313 default: gdb_assert_not_reached ("bad domain_enum");
320 domain_name (domain_search_flags flags
)
322 static constexpr domain_search_flags::string_mapping mapping
[] = {
323 #define SYM_DOMAIN(X) \
324 MAP_ENUM_FLAG (SEARCH_ ## X ## _DOMAIN),
325 #include "sym-domains.def"
329 return flags
.to_string (mapping
);
335 from_scripting_domain (int val
)
337 if ((val
& SCRIPTING_SEARCH_FLAG
) == 0)
339 /* VAL should be one of the domain constants. Verify this and
340 convert it to a search constant. */
343 #define SYM_DOMAIN(X) \
344 case X ## _DOMAIN: break;
345 #include "sym-domains.def"
348 error (_("unrecognized domain constant"));
350 domain_search_flags result
= to_search_flags ((domain_enum
) val
);
351 if (val
== VAR_DOMAIN
)
353 /* This matches the historical practice. */
354 result
|= SEARCH_TYPE_DOMAIN
| SEARCH_FUNCTION_DOMAIN
;
360 /* VAL is several search constants or'd together. Verify
362 val
&= ~SCRIPTING_SEARCH_FLAG
;
364 #define SYM_DOMAIN(X) \
365 check &= ~ (int) SEARCH_ ## X ## _DOMAIN;
366 #include "sym-domains.def"
369 error (_("unrecognized domain constant"));
370 return (domain_search_flag
) val
;
377 search_symbol_list (const char *name
, int num
, struct symbol
**syms
)
379 for (int i
= 0; i
< num
; ++i
)
381 if (strcmp (name
, syms
[i
]->natural_name ()) == 0)
390 linetable_entry::pc (const struct objfile
*objfile
) const
392 return CORE_ADDR (m_pc
) + objfile
->text_section_offset ();
398 compunit_symtab::find_call_site (CORE_ADDR pc
) const
400 if (m_call_site_htab
== nullptr)
403 CORE_ADDR delta
= this->objfile ()->text_section_offset ();
405 if (auto it
= m_call_site_htab
->find (static_cast<unrelocated_addr
> (pc
- delta
));
406 it
!= m_call_site_htab
->end ())
409 /* See if the arch knows another PC we should try. On some
410 platforms, GCC emits a DWARF call site that is offset from the
411 actual return location. */
412 struct gdbarch
*arch
= objfile ()->arch ();
413 CORE_ADDR new_pc
= gdbarch_update_call_site_pc (arch
, pc
);
418 if (auto it
= m_call_site_htab
->find (static_cast<unrelocated_addr
> (new_pc
- delta
));
419 it
!= m_call_site_htab
->end ())
428 compunit_symtab::set_blockvector
429 (std::unique_ptr
<struct blockvector
> blockvector
)
431 gdb_assert (m_blockvector
== nullptr);
432 m_blockvector
= std::move (blockvector
);
438 compunit_symtab::set_call_site_htab (call_site_htab_t
&&call_site_htab
)
440 gdb_assert (m_call_site_htab
== nullptr);
441 if (!call_site_htab
.empty ())
443 = std::make_unique
<call_site_htab_t
> (std::move (call_site_htab
));
449 compunit_symtab::set_primary_filetab (symtab
*primary_filetab
)
451 symtab
*prev_filetab
= nullptr;
453 /* Move PRIMARY_FILETAB to the head of the filetab list. */
454 for (symtab
*filetab
: this->filetabs ())
456 if (filetab
== primary_filetab
)
458 if (prev_filetab
!= nullptr)
460 prev_filetab
->next
= primary_filetab
->next
;
461 primary_filetab
->next
= m_filetabs
;
462 m_filetabs
= primary_filetab
;
468 prev_filetab
= filetab
;
471 gdb_assert (primary_filetab
== m_filetabs
);
477 compunit_symtab::primary_filetab () const
479 gdb_assert (m_filetabs
!= nullptr);
481 /* The primary file symtab is the first one in the list. */
488 compunit_symtab::language () const
490 struct symtab
*symtab
= primary_filetab ();
492 /* The language of the compunit symtab is the language of its
493 primary source file. */
494 return symtab
->language ();
500 compunit_symtab::forget_cached_source_info ()
502 for (symtab
*s
: filetabs ())
503 s
->release_fullname ();
509 compunit_symtab::symbol_at_address (CORE_ADDR addr
) const
511 return blockvector ()->symbol_at_address (addr
);
516 compunit_symtab::compunit_symtab (struct objfile
*objfile
,
518 : m_objfile (objfile
),
519 /* The name we record here is only for display/debugging purposes.
520 Just save the basename to avoid path issues (too long for
521 display, relative vs absolute, etc.). */
522 name (obstack_strdup (&objfile
->objfile_obstack
, lbasename (name_
))),
523 m_locations_valid (false),
524 m_epilogue_unwind_valid (false)
526 symtab_create_debug_printf_v ("created compunit symtab %s for %s",
527 host_address_to_string (this),
533 compunit_symtab::~compunit_symtab ()
535 this->forget_cached_source_info ();
538 /* The relocated address of the minimal symbol, using the section
539 offsets from OBJFILE. */
542 minimal_symbol::value_address (objfile
*objfile
) const
544 if (this->maybe_copied (objfile
))
545 return this->get_maybe_copied_address (objfile
);
547 return (CORE_ADDR (this->unrelocated_address ())
548 + objfile
->section_offsets
[this->section_index ()]);
554 minimal_symbol::data_p () const
556 return m_type
== mst_data
559 || m_type
== mst_file_data
560 || m_type
== mst_file_bss
;
566 minimal_symbol::text_p () const
568 return m_type
== mst_text
569 || m_type
== mst_text_gnu_ifunc
570 || m_type
== mst_data_gnu_ifunc
571 || m_type
== mst_slot_got_plt
572 || m_type
== mst_solib_trampoline
573 || m_type
== mst_file_text
;
579 minimal_symbol::maybe_copied (objfile
*objfile
) const
581 return (objfile
->object_format_has_copy_relocs
582 && (objfile
->flags
& OBJF_MAINLINE
) == 0
583 && (m_type
== mst_data
|| m_type
== mst_bss
));
586 /* See whether FILENAME matches SEARCH_NAME using the rule that we
587 advertise to the user. (The manual's description of linespecs
588 describes what we advertise). Returns true if they match, false
592 compare_filenames_for_search (const char *filename
, const char *search_name
)
594 int len
= strlen (filename
);
595 size_t search_len
= strlen (search_name
);
597 if (len
< search_len
)
600 /* The tail of FILENAME must match. */
601 if (FILENAME_CMP (filename
+ len
- search_len
, search_name
) != 0)
604 /* Either the names must completely match, or the character
605 preceding the trailing SEARCH_NAME segment of FILENAME must be a
608 The check !IS_ABSOLUTE_PATH ensures SEARCH_NAME "/dir/file.c"
609 cannot match FILENAME "/path//dir/file.c" - as user has requested
610 absolute path. The sama applies for "c:\file.c" possibly
611 incorrectly hypothetically matching "d:\dir\c:\file.c".
613 The HAS_DRIVE_SPEC purpose is to make FILENAME "c:file.c"
614 compatible with SEARCH_NAME "file.c". In such case a compiler had
615 to put the "c:file.c" name into debug info. Such compatibility
616 works only on GDB built for DOS host. */
617 return (len
== search_len
618 || (!IS_ABSOLUTE_PATH (search_name
)
619 && IS_DIR_SEPARATOR (filename
[len
- search_len
- 1]))
620 || (HAS_DRIVE_SPEC (filename
)
621 && STRIP_DRIVE_SPEC (filename
) == &filename
[len
- search_len
]));
627 iterate_over_symtabs (program_space
*pspace
, const char *name
,
628 gdb::function_view
<bool (symtab
*)> callback
)
630 gdb::unique_xmalloc_ptr
<char> real_path
;
632 /* Here we are interested in canonicalizing an absolute path, not
633 absolutizing a relative path. */
634 if (IS_ABSOLUTE_PATH (name
))
636 real_path
= gdb_realpath (name
);
637 gdb_assert (IS_ABSOLUTE_PATH (real_path
.get ()));
640 for (objfile
&objfile
: pspace
->objfiles ())
641 if (objfile
.map_symtabs_matching_filename (name
, real_path
.get (),
649 lookup_symtab (program_space
*pspace
, const char *name
)
651 struct symtab
*result
= NULL
;
653 iterate_over_symtabs (pspace
, name
, [&] (symtab
*symtab
)
663 /* Mangle a GDB method stub type. This actually reassembles the pieces of the
664 full method name, which consist of the class name (from T), the unadorned
665 method name from METHOD_ID, and the signature for the specific overload,
666 specified by SIGNATURE_ID. Note that this function is g++ specific. */
669 gdb_mangle_name (struct type
*type
, int method_id
, int signature_id
)
671 int mangled_name_len
;
673 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, method_id
);
674 struct fn_field
*method
= &f
[signature_id
];
675 const char *field_name
= TYPE_FN_FIELDLIST_NAME (type
, method_id
);
676 const char *physname
= TYPE_FN_FIELD_PHYSNAME (f
, signature_id
);
677 const char *newname
= type
->name ();
679 /* Does the form of physname indicate that it is the full mangled name
680 of a constructor (not just the args)? */
681 int is_full_physname_constructor
;
684 int is_destructor
= is_destructor_name (physname
);
685 /* Need a new type prefix. */
686 const char *const_prefix
= method
->is_const
? "C" : "";
687 const char *volatile_prefix
= method
->is_volatile
? "V" : "";
689 int len
= (newname
== NULL
? 0 : strlen (newname
));
691 /* Nothing to do if physname already contains a fully mangled v3 abi name
692 or an operator name. */
693 if ((physname
[0] == '_' && physname
[1] == 'Z')
694 || is_operator_name (field_name
))
695 return xstrdup (physname
);
697 is_full_physname_constructor
= is_constructor_name (physname
);
699 is_constructor
= is_full_physname_constructor
700 || (newname
&& strcmp (field_name
, newname
) == 0);
703 is_destructor
= (startswith (physname
, "__dt"));
705 if (is_destructor
|| is_full_physname_constructor
)
707 mangled_name
= (char *) xmalloc (strlen (physname
) + 1);
708 strcpy (mangled_name
, physname
);
714 xsnprintf (buf
, sizeof (buf
), "__%s%s", const_prefix
, volatile_prefix
);
716 else if (physname
[0] == 't' || physname
[0] == 'Q')
718 /* The physname for template and qualified methods already includes
720 xsnprintf (buf
, sizeof (buf
), "__%s%s", const_prefix
, volatile_prefix
);
726 xsnprintf (buf
, sizeof (buf
), "__%s%s%d", const_prefix
,
727 volatile_prefix
, len
);
729 mangled_name_len
= ((is_constructor
? 0 : strlen (field_name
))
730 + strlen (buf
) + len
+ strlen (physname
) + 1);
732 mangled_name
= (char *) xmalloc (mangled_name_len
);
734 mangled_name
[0] = '\0';
736 strcpy (mangled_name
, field_name
);
738 strcat (mangled_name
, buf
);
739 /* If the class doesn't have a name, i.e. newname NULL, then we just
740 mangle it using 0 for the length of the class. Thus it gets mangled
741 as something starting with `::' rather than `classname::'. */
743 strcat (mangled_name
, newname
);
745 strcat (mangled_name
, physname
);
746 return (mangled_name
);
752 general_symbol_info::set_demangled_name (const char *name
,
753 struct obstack
*obstack
)
755 if (language () == language_ada
)
760 language_specific
.obstack
= obstack
;
765 language_specific
.demangled_name
= name
;
769 language_specific
.demangled_name
= name
;
773 /* Initialize the language dependent portion of a symbol
774 depending upon the language for the symbol. */
777 general_symbol_info::set_language (enum language language
,
778 struct obstack
*obstack
)
780 m_language
= language
;
781 if (language
== language_cplus
782 || language
== language_d
783 || language
== language_go
784 || language
== language_objc
785 || language
== language_fortran
)
787 set_demangled_name (NULL
, obstack
);
789 else if (language
== language_ada
)
791 gdb_assert (ada_mangled
== 0);
792 language_specific
.obstack
= obstack
;
796 memset (&language_specific
, 0, sizeof (language_specific
));
800 /* Functions to initialize a symbol's mangled name. */
802 /* Objects of this type are stored in the demangled name hash table. */
803 struct demangled_name_entry
805 demangled_name_entry (std::string_view mangled_name
)
806 : mangled (mangled_name
) {}
808 std::string_view mangled
;
809 enum language language
;
810 gdb::unique_xmalloc_ptr
<char> demangled
;
813 /* Hash function for the demangled name hash. */
816 hash_demangled_name_entry (const void *data
)
818 const struct demangled_name_entry
*e
819 = (const struct demangled_name_entry
*) data
;
821 return gdb::string_view_hash () (e
->mangled
);
824 /* Equality function for the demangled name hash. */
827 eq_demangled_name_entry (const void *a
, const void *b
)
829 const struct demangled_name_entry
*da
830 = (const struct demangled_name_entry
*) a
;
831 const struct demangled_name_entry
*db
832 = (const struct demangled_name_entry
*) b
;
834 return da
->mangled
== db
->mangled
;
838 free_demangled_name_entry (void *data
)
840 struct demangled_name_entry
*e
841 = (struct demangled_name_entry
*) data
;
843 e
->~demangled_name_entry();
846 /* Create the hash table used for demangled names. Each hash entry is
847 a pair of strings; one for the mangled name and one for the demangled
848 name. The entry is hashed via just the mangled name. */
851 create_demangled_names_hash (struct objfile_per_bfd_storage
*per_bfd
)
853 /* Choose 256 as the starting size of the hash table, somewhat arbitrarily.
854 The hash table code will round this up to the next prime number.
855 Choosing a much larger table size wastes memory, and saves only about
856 1% in symbol reading. However, if the minsym count is already
857 initialized (e.g. because symbol name setting was deferred to
858 a background thread) we can initialize the hashtable with a count
859 based on that, because we will almost certainly have at least that
860 many entries. If we have a nonzero number but less than 256,
861 we still stay with 256 to have some space for psymbols, etc. */
863 /* htab will expand the table when it is 3/4th full, so we account for that
864 here. +2 to round up. */
865 int minsym_based_count
= (per_bfd
->minimal_symbol_count
+ 2) / 3 * 4;
866 int count
= std::max (per_bfd
->minimal_symbol_count
, minsym_based_count
);
868 per_bfd
->demangled_names_hash
.reset (htab_create_alloc
869 (count
, hash_demangled_name_entry
, eq_demangled_name_entry
,
870 free_demangled_name_entry
, xcalloc
, xfree
));
875 gdb::unique_xmalloc_ptr
<char>
876 symbol_find_demangled_name (struct general_symbol_info
*gsymbol
,
879 gdb::unique_xmalloc_ptr
<char> demangled
;
882 if (gsymbol
->language () != language_unknown
)
884 const struct language_defn
*lang
= language_def (gsymbol
->language ());
886 lang
->sniff_from_mangled_name (mangled
, &demangled
);
890 for (i
= language_unknown
; i
< nr_languages
; ++i
)
892 enum language l
= (enum language
) i
;
893 const struct language_defn
*lang
= language_def (l
);
895 if (lang
->sniff_from_mangled_name (mangled
, &demangled
))
897 gsymbol
->m_language
= l
;
905 /* Set both the mangled and demangled (if any) names for GSYMBOL based
906 on LINKAGE_NAME and LEN. Ordinarily, NAME is copied onto the
907 objfile's obstack; but if COPY_NAME is 0 and if NAME is
908 NUL-terminated, then this function assumes that NAME is already
909 correctly saved (either permanently or with a lifetime tied to the
910 objfile), and it will not be copied.
912 The hash table corresponding to OBJFILE is used, and the memory
913 comes from the per-BFD storage_obstack. LINKAGE_NAME is copied,
914 so the pointer can be discarded after calling this function. */
917 general_symbol_info::compute_and_set_names (std::string_view linkage_name
,
919 objfile_per_bfd_storage
*per_bfd
,
920 std::optional
<hashval_t
> hash
)
922 struct demangled_name_entry
**slot
;
924 if (language () == language_ada
)
926 /* In Ada, we do the symbol lookups using the mangled name, so
927 we can save some space by not storing the demangled name. */
929 m_name
= linkage_name
.data ();
931 m_name
= obstack_strndup (&per_bfd
->storage_obstack
,
932 linkage_name
.data (),
933 linkage_name
.length ());
934 set_demangled_name (NULL
, &per_bfd
->storage_obstack
);
939 if (per_bfd
->demangled_names_hash
== NULL
)
940 create_demangled_names_hash (per_bfd
);
942 struct demangled_name_entry
entry (linkage_name
);
943 if (!hash
.has_value ())
944 hash
= hash_demangled_name_entry (&entry
);
945 slot
= ((struct demangled_name_entry
**)
946 htab_find_slot_with_hash (per_bfd
->demangled_names_hash
.get (),
947 &entry
, *hash
, INSERT
));
949 /* The const_cast is safe because the only reason it is already
950 initialized is if we purposefully set it from a background
951 thread to avoid doing the work here. However, it is still
952 allocated from the heap and needs to be freed by us, just
953 like if we called symbol_find_demangled_name here. If this is
954 nullptr, we call symbol_find_demangled_name below, but we put
955 this smart pointer here to be sure that we don't leak this name. */
956 gdb::unique_xmalloc_ptr
<char> demangled_name
957 (const_cast<char *> (language_specific
.demangled_name
));
959 /* If this name is not in the hash table, add it. */
961 /* A C version of the symbol may have already snuck into the table.
962 This happens to, e.g., main.init (__go_init_main). Cope. */
963 || (language () == language_go
&& (*slot
)->demangled
== nullptr))
965 /* A 0-terminated copy of the linkage name. Callers must set COPY_NAME
966 to true if the string might not be nullterminated. We have to make
967 this copy because demangling needs a nullterminated string. */
968 std::string_view linkage_name_copy
;
971 char *alloc_name
= (char *) alloca (linkage_name
.length () + 1);
972 memcpy (alloc_name
, linkage_name
.data (), linkage_name
.length ());
973 alloc_name
[linkage_name
.length ()] = '\0';
975 linkage_name_copy
= std::string_view (alloc_name
,
976 linkage_name
.length ());
979 linkage_name_copy
= linkage_name
;
981 if (demangled_name
.get () == nullptr)
983 = symbol_find_demangled_name (this, linkage_name_copy
.data ());
985 /* Suppose we have demangled_name==NULL, copy_name==0, and
986 linkage_name_copy==linkage_name. In this case, we already have the
987 mangled name saved, and we don't have a demangled name. So,
988 you might think we could save a little space by not recording
989 this in the hash table at all.
991 It turns out that it is actually important to still save such
992 an entry in the hash table, because storing this name gives
993 us better bcache hit rates for partial symbols. */
997 = ((struct demangled_name_entry
*)
998 obstack_alloc (&per_bfd
->storage_obstack
,
999 sizeof (demangled_name_entry
)));
1000 new (*slot
) demangled_name_entry (linkage_name
);
1004 /* If we must copy the mangled name, put it directly after
1005 the struct so we can have a single allocation. */
1007 = ((struct demangled_name_entry
*)
1008 obstack_alloc (&per_bfd
->storage_obstack
,
1009 sizeof (demangled_name_entry
)
1010 + linkage_name
.length () + 1));
1011 char *mangled_ptr
= reinterpret_cast<char *> (*slot
+ 1);
1012 memcpy (mangled_ptr
, linkage_name
.data (), linkage_name
.length ());
1013 mangled_ptr
[linkage_name
.length ()] = '\0';
1014 new (*slot
) demangled_name_entry
1015 (std::string_view (mangled_ptr
, linkage_name
.length ()));
1017 (*slot
)->demangled
= std::move (demangled_name
);
1018 (*slot
)->language
= language ();
1020 else if (language () == language_unknown
)
1021 m_language
= (*slot
)->language
;
1023 m_name
= (*slot
)->mangled
.data ();
1024 set_demangled_name ((*slot
)->demangled
.get (), &per_bfd
->storage_obstack
);
1030 general_symbol_info::natural_name () const
1032 switch (language ())
1034 case language_cplus
:
1038 case language_fortran
:
1040 if (language_specific
.demangled_name
!= nullptr)
1041 return language_specific
.demangled_name
;
1044 return ada_decode_symbol (this);
1048 return linkage_name ();
1054 general_symbol_info::demangled_name () const
1056 const char *dem_name
= NULL
;
1058 switch (language ())
1060 case language_cplus
:
1064 case language_fortran
:
1066 dem_name
= language_specific
.demangled_name
;
1069 dem_name
= ada_decode_symbol (this);
1080 general_symbol_info::search_name () const
1082 if (language () == language_ada
)
1083 return linkage_name ();
1085 return natural_name ();
1090 struct obj_section
*
1091 general_symbol_info::obj_section (const struct objfile
*objfile
) const
1093 if (section_index () >= 0)
1094 return &objfile
->sections_start
[section_index ()];
1101 symbol_matches_search_name (const struct general_symbol_info
*gsymbol
,
1102 const lookup_name_info
&name
)
1104 symbol_name_matcher_ftype
*name_match
1105 = language_def (gsymbol
->language ())->get_symbol_name_matcher (name
);
1106 return name_match (gsymbol
->search_name (), name
, NULL
);
1111 /* Return true if the two sections are the same, or if they could
1112 plausibly be copies of each other, one in an original object
1113 file and another in a separated debug file. */
1116 matching_obj_sections (struct obj_section
*obj_first
,
1117 struct obj_section
*obj_second
)
1119 asection
*first
= obj_first
? obj_first
->the_bfd_section
: NULL
;
1120 asection
*second
= obj_second
? obj_second
->the_bfd_section
: NULL
;
1122 /* If they're the same section, then they match. */
1123 if (first
== second
)
1126 /* If either is NULL, give up. */
1127 if (first
== NULL
|| second
== NULL
)
1130 /* This doesn't apply to absolute symbols. */
1131 if (first
->owner
== NULL
|| second
->owner
== NULL
)
1134 /* If they're in the same object file, they must be different sections. */
1135 if (first
->owner
== second
->owner
)
1138 /* Check whether the two sections are potentially corresponding. They must
1139 have the same size, address, and name. We can't compare section indexes,
1140 which would be more reliable, because some sections may have been
1142 if (bfd_section_size (first
) != bfd_section_size (second
))
1145 /* In-memory addresses may start at a different offset, relativize them. */
1146 if (bfd_section_vma (first
) - bfd_get_start_address (first
->owner
)
1147 != bfd_section_vma (second
) - bfd_get_start_address (second
->owner
))
1150 if (bfd_section_name (first
) == NULL
1151 || bfd_section_name (second
) == NULL
1152 || strcmp (bfd_section_name (first
), bfd_section_name (second
)) != 0)
1155 /* Otherwise check that they are in corresponding objfiles. */
1157 struct objfile
*obj
= NULL
;
1158 for (objfile
&objfile
: current_program_space
->objfiles ())
1159 if (objfile
.obfd
== first
->owner
)
1164 gdb_assert (obj
!= NULL
);
1166 if (obj
->separate_debug_objfile
!= NULL
1167 && obj
->separate_debug_objfile
->obfd
== second
->owner
)
1169 if (obj
->separate_debug_objfile_backlink
!= NULL
1170 && obj
->separate_debug_objfile_backlink
->obfd
== second
->owner
)
1176 /* Hash function for the symbol cache. */
1179 hash_symbol_entry (const struct objfile
*objfile_context
,
1180 const char *name
, domain_search_flags domain
)
1182 unsigned int hash
= (uintptr_t) objfile_context
;
1185 hash
+= htab_hash_string (name
);
1192 /* Equality function for the symbol cache. */
1195 eq_symbol_entry (const struct symbol_cache_slot
*slot
,
1196 const struct objfile
*objfile_context
,
1197 const char *name
, domain_search_flags domain
)
1199 const char *slot_name
;
1201 if (slot
->state
== SYMBOL_SLOT_UNUSED
)
1204 if (slot
->objfile_context
!= objfile_context
)
1207 domain_search_flags slot_domain
= slot
->domain
;
1208 if (slot
->state
== SYMBOL_SLOT_NOT_FOUND
)
1209 slot_name
= slot
->value
.name
;
1211 slot_name
= slot
->value
.found
.symbol
->search_name ();
1213 /* NULL names match. */
1214 if (slot_name
== NULL
&& name
== NULL
)
1216 /* But there's no point in calling symbol_matches_domain in the
1217 SYMBOL_SLOT_FOUND case. */
1218 if (slot_domain
!= domain
)
1221 else if (slot_name
!= NULL
&& name
!= NULL
)
1223 /* It's important that we use the same comparison that was done
1224 the first time through. If the slot records a found symbol,
1225 then this means using the symbol name comparison function of
1226 the symbol's language with symbol->search_name (). See
1229 If the slot records a not-found symbol, then require a precise match.
1230 We could still be lax with whitespace like strcmp_iw though. */
1232 if (slot_domain
!= domain
)
1235 if (slot
->state
== SYMBOL_SLOT_NOT_FOUND
)
1237 if (strcmp (slot_name
, name
) != 0)
1242 struct symbol
*sym
= slot
->value
.found
.symbol
;
1243 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
1245 if (!symbol_matches_search_name (sym
, lookup_name
))
1251 /* Only one name is NULL. */
1258 /* Given a cache of size SIZE, return the size of the struct (with variable
1259 length array) in bytes. */
1262 symbol_cache_byte_size (unsigned int size
)
1264 return (sizeof (struct block_symbol_cache
)
1265 + ((size
- 1) * sizeof (struct symbol_cache_slot
)));
1271 resize_symbol_cache (struct symbol_cache
*cache
, unsigned int new_size
)
1273 /* If there's no change in size, don't do anything.
1274 All caches have the same size, so we can just compare with the size
1275 of the global symbols cache. */
1276 if ((cache
->global_symbols
!= NULL
1277 && cache
->global_symbols
->size
== new_size
)
1278 || (cache
->global_symbols
== NULL
1282 destroy_block_symbol_cache (cache
->global_symbols
);
1283 destroy_block_symbol_cache (cache
->static_symbols
);
1287 cache
->global_symbols
= NULL
;
1288 cache
->static_symbols
= NULL
;
1292 size_t total_size
= symbol_cache_byte_size (new_size
);
1294 cache
->global_symbols
1295 = (struct block_symbol_cache
*) xcalloc (1, total_size
);
1296 cache
->static_symbols
1297 = (struct block_symbol_cache
*) xcalloc (1, total_size
);
1298 cache
->global_symbols
->size
= new_size
;
1299 cache
->static_symbols
->size
= new_size
;
1303 /* Return the symbol cache of PSPACE.
1304 Create one if it doesn't exist yet. */
1306 static struct symbol_cache
*
1307 get_symbol_cache (struct program_space
*pspace
)
1309 struct symbol_cache
*cache
= symbol_cache_key
.get (pspace
);
1313 cache
= symbol_cache_key
.emplace (pspace
);
1314 resize_symbol_cache (cache
, symbol_cache_size
);
1320 /* Set the size of the symbol cache in all program spaces. */
1323 set_symbol_cache_size (unsigned int new_size
)
1325 for (struct program_space
*pspace
: program_spaces
)
1327 struct symbol_cache
*cache
= symbol_cache_key
.get (pspace
);
1329 /* The pspace could have been created but not have a cache yet. */
1331 resize_symbol_cache (cache
, new_size
);
1335 /* Called when symbol-cache-size is set. */
1338 set_symbol_cache_size_handler (const char *args
, int from_tty
,
1339 struct cmd_list_element
*c
)
1341 if (new_symbol_cache_size
> MAX_SYMBOL_CACHE_SIZE
)
1343 /* Restore the previous value.
1344 This is the value the "show" command prints. */
1345 new_symbol_cache_size
= symbol_cache_size
;
1347 error (_("Symbol cache size is too large, max is %u."),
1348 MAX_SYMBOL_CACHE_SIZE
);
1350 symbol_cache_size
= new_symbol_cache_size
;
1352 set_symbol_cache_size (symbol_cache_size
);
1355 /* Lookup symbol NAME,DOMAIN in BLOCK in the symbol cache of PSPACE.
1356 OBJFILE_CONTEXT is the current objfile, which may be NULL.
1357 The result is the symbol if found, SYMBOL_LOOKUP_FAILED if a previous lookup
1358 failed (and thus this one will too), or NULL if the symbol is not present
1360 *BSC_PTR and *SLOT_PTR are set to the cache and slot of the symbol, which
1361 can be used to save the result of a full lookup attempt. */
1363 static struct block_symbol
1364 symbol_cache_lookup (struct symbol_cache
*cache
,
1365 struct objfile
*objfile_context
, enum block_enum block
,
1366 const char *name
, domain_search_flags domain
,
1367 struct block_symbol_cache
**bsc_ptr
,
1368 struct symbol_cache_slot
**slot_ptr
)
1370 struct block_symbol_cache
*bsc
;
1372 struct symbol_cache_slot
*slot
;
1374 if (block
== GLOBAL_BLOCK
)
1375 bsc
= cache
->global_symbols
;
1377 bsc
= cache
->static_symbols
;
1385 hash
= hash_symbol_entry (objfile_context
, name
, domain
);
1386 slot
= bsc
->symbols
+ hash
% bsc
->size
;
1391 if (eq_symbol_entry (slot
, objfile_context
, name
, domain
))
1393 symbol_lookup_debug_printf ("%s block symbol cache hit%s for %s, %s",
1394 block
== GLOBAL_BLOCK
? "Global" : "Static",
1395 slot
->state
== SYMBOL_SLOT_NOT_FOUND
1396 ? " (not found)" : "", name
,
1397 domain_name (domain
).c_str ());
1399 if (slot
->state
== SYMBOL_SLOT_NOT_FOUND
)
1400 return SYMBOL_LOOKUP_FAILED
;
1401 return slot
->value
.found
;
1404 /* Symbol is not present in the cache. */
1406 symbol_lookup_debug_printf ("%s block symbol cache miss for %s, %s",
1407 block
== GLOBAL_BLOCK
? "Global" : "Static",
1408 name
, domain_name (domain
).c_str ());
1413 /* Mark SYMBOL as found in SLOT.
1414 OBJFILE_CONTEXT is the current objfile when the lookup was done, or NULL
1415 if it's not needed to distinguish lookups (STATIC_BLOCK). It is *not*
1416 necessarily the objfile the symbol was found in. */
1419 symbol_cache_mark_found (struct block_symbol_cache
*bsc
,
1420 struct symbol_cache_slot
*slot
,
1421 struct objfile
*objfile_context
,
1422 struct symbol
*symbol
,
1423 const struct block
*block
,
1424 domain_search_flags domain
)
1428 if (slot
->state
!= SYMBOL_SLOT_UNUSED
)
1431 symbol_cache_clear_slot (slot
);
1433 slot
->state
= SYMBOL_SLOT_FOUND
;
1434 slot
->objfile_context
= objfile_context
;
1435 slot
->value
.found
.symbol
= symbol
;
1436 slot
->value
.found
.block
= block
;
1437 slot
->domain
= domain
;
1440 /* Mark symbol NAME, DOMAIN as not found in SLOT.
1441 OBJFILE_CONTEXT is the current objfile when the lookup was done, or NULL
1442 if it's not needed to distinguish lookups (STATIC_BLOCK). */
1445 symbol_cache_mark_not_found (struct block_symbol_cache
*bsc
,
1446 struct symbol_cache_slot
*slot
,
1447 struct objfile
*objfile_context
,
1448 const char *name
, domain_search_flags domain
)
1452 if (slot
->state
!= SYMBOL_SLOT_UNUSED
)
1455 symbol_cache_clear_slot (slot
);
1457 slot
->state
= SYMBOL_SLOT_NOT_FOUND
;
1458 slot
->objfile_context
= objfile_context
;
1459 slot
->value
.name
= xstrdup (name
);
1460 slot
->domain
= domain
;
1463 /* Flush the symbol cache of PSPACE. */
1466 symbol_cache_flush (struct program_space
*pspace
)
1468 ada_clear_symbol_cache (pspace
);
1469 struct symbol_cache
*cache
= symbol_cache_key
.get (pspace
);
1474 if (cache
->global_symbols
== NULL
)
1476 gdb_assert (symbol_cache_size
== 0);
1477 gdb_assert (cache
->static_symbols
== NULL
);
1481 /* If the cache is untouched since the last flush, early exit.
1482 This is important for performance during the startup of a program linked
1483 with 100s (or 1000s) of shared libraries. */
1484 if (cache
->global_symbols
->misses
== 0
1485 && cache
->static_symbols
->misses
== 0)
1488 gdb_assert (cache
->global_symbols
->size
== symbol_cache_size
);
1489 gdb_assert (cache
->static_symbols
->size
== symbol_cache_size
);
1491 for (pass
= 0; pass
< 2; ++pass
)
1493 struct block_symbol_cache
*bsc
1494 = pass
== 0 ? cache
->global_symbols
: cache
->static_symbols
;
1497 for (i
= 0; i
< bsc
->size
; ++i
)
1498 symbol_cache_clear_slot (&bsc
->symbols
[i
]);
1501 cache
->global_symbols
->hits
= 0;
1502 cache
->global_symbols
->misses
= 0;
1503 cache
->global_symbols
->collisions
= 0;
1504 cache
->static_symbols
->hits
= 0;
1505 cache
->static_symbols
->misses
= 0;
1506 cache
->static_symbols
->collisions
= 0;
1512 symbol_cache_dump (const struct symbol_cache
*cache
)
1516 if (cache
->global_symbols
== NULL
)
1518 gdb_printf (" <disabled>\n");
1522 for (pass
= 0; pass
< 2; ++pass
)
1524 const struct block_symbol_cache
*bsc
1525 = pass
== 0 ? cache
->global_symbols
: cache
->static_symbols
;
1529 gdb_printf ("Global symbols:\n");
1531 gdb_printf ("Static symbols:\n");
1533 for (i
= 0; i
< bsc
->size
; ++i
)
1535 const struct symbol_cache_slot
*slot
= &bsc
->symbols
[i
];
1539 switch (slot
->state
)
1541 case SYMBOL_SLOT_UNUSED
:
1543 case SYMBOL_SLOT_NOT_FOUND
:
1544 gdb_printf (" [%4u] = %s, %s %s (not found)\n", i
,
1545 host_address_to_string (slot
->objfile_context
),
1547 domain_name (slot
->domain
).c_str ());
1549 case SYMBOL_SLOT_FOUND
:
1551 struct symbol
*found
= slot
->value
.found
.symbol
;
1552 const struct objfile
*context
= slot
->objfile_context
;
1554 gdb_printf (" [%4u] = %s, %s %s\n", i
,
1555 host_address_to_string (context
),
1556 found
->print_name (),
1557 domain_name (found
->domain ()));
1565 /* The "mt print symbol-cache" command. */
1568 maintenance_print_symbol_cache (const char *args
, int from_tty
)
1570 for (struct program_space
*pspace
: program_spaces
)
1572 struct symbol_cache
*cache
;
1574 gdb_printf (_("Symbol cache for pspace %d\n%s:\n"),
1576 pspace
->symfile_object_file
!= NULL
1577 ? objfile_name (pspace
->symfile_object_file
)
1578 : "(no object file)");
1580 /* If the cache hasn't been created yet, avoid creating one. */
1581 cache
= symbol_cache_key
.get (pspace
);
1583 gdb_printf (" <empty>\n");
1585 symbol_cache_dump (cache
);
1589 /* The "mt flush-symbol-cache" command. */
1592 maintenance_flush_symbol_cache (const char *args
, int from_tty
)
1594 for (struct program_space
*pspace
: program_spaces
)
1596 symbol_cache_flush (pspace
);
1600 /* Print usage statistics of CACHE. */
1603 symbol_cache_stats (struct symbol_cache
*cache
)
1607 if (cache
->global_symbols
== NULL
)
1609 gdb_printf (" <disabled>\n");
1613 for (pass
= 0; pass
< 2; ++pass
)
1615 const struct block_symbol_cache
*bsc
1616 = pass
== 0 ? cache
->global_symbols
: cache
->static_symbols
;
1621 gdb_printf ("Global block cache stats:\n");
1623 gdb_printf ("Static block cache stats:\n");
1625 gdb_printf (" size: %u\n", bsc
->size
);
1626 gdb_printf (" hits: %u\n", bsc
->hits
);
1627 gdb_printf (" misses: %u\n", bsc
->misses
);
1628 gdb_printf (" collisions: %u\n", bsc
->collisions
);
1632 /* The "mt print symbol-cache-statistics" command. */
1635 maintenance_print_symbol_cache_statistics (const char *args
, int from_tty
)
1637 for (struct program_space
*pspace
: program_spaces
)
1639 struct symbol_cache
*cache
;
1641 gdb_printf (_("Symbol cache statistics for pspace %d\n%s:\n"),
1643 pspace
->symfile_object_file
!= NULL
1644 ? objfile_name (pspace
->symfile_object_file
)
1645 : "(no object file)");
1647 /* If the cache hasn't been created yet, avoid creating one. */
1648 cache
= symbol_cache_key
.get (pspace
);
1650 gdb_printf (" empty, no stats available\n");
1652 symbol_cache_stats (cache
);
1656 /* This module's 'new_objfile' observer. */
1659 symtab_new_objfile_observer (struct objfile
*objfile
)
1661 symbol_cache_flush (objfile
->pspace ());
1664 /* This module's 'all_objfiles_removed' observer. */
1667 symtab_all_objfiles_removed (program_space
*pspace
)
1669 symbol_cache_flush (pspace
);
1671 /* Forget everything we know about the main function. */
1672 main_progspace_key
.clear (pspace
);
1675 /* This module's 'free_objfile' observer. */
1678 symtab_free_objfile_observer (struct objfile
*objfile
)
1680 symbol_cache_flush (objfile
->pspace ());
1686 fixup_symbol_section (struct symbol
*sym
, struct objfile
*objfile
)
1688 gdb_assert (sym
!= nullptr);
1689 gdb_assert (sym
->is_objfile_owned ());
1690 gdb_assert (objfile
!= nullptr);
1691 gdb_assert (sym
->section_index () == -1);
1693 /* Note that if this ends up as -1, fixup_section will handle that
1694 reasonably well. So, it's fine to use the objfile's section
1695 index without doing the check that is done by the wrapper macros
1696 like SECT_OFF_TEXT. */
1698 switch (sym
->loc_class ())
1701 fallback
= objfile
->sect_index_data
;
1705 fallback
= objfile
->sect_index_text
;
1709 /* Nothing else will be listed in the minsyms -- no use looking
1714 CORE_ADDR addr
= sym
->value_address ();
1716 struct minimal_symbol
*msym
;
1718 /* First, check whether a minimal symbol with the same name exists
1719 and points to the same address. The address check is required
1720 e.g. on PowerPC64, where the minimal symbol for a function will
1721 point to the function descriptor, while the debug symbol will
1722 point to the actual function code. */
1723 msym
= lookup_minimal_symbol_by_pc_name (addr
, sym
->linkage_name (),
1726 sym
->set_section_index (msym
->section_index ());
1729 /* Static, function-local variables do appear in the linker
1730 (minimal) symbols, but are frequently given names that won't
1731 be found via lookup_minimal_symbol(). E.g., it has been
1732 observed in frv-uclinux (ELF) executables that a static,
1733 function-local variable named "foo" might appear in the
1734 linker symbols as "foo.6" or "foo.3". Thus, there is no
1735 point in attempting to extend the lookup-by-name mechanism to
1736 handle this case due to the fact that there can be multiple
1739 So, instead, search the section table when lookup by name has
1740 failed. The ``addr'' and ``endaddr'' fields may have already
1741 been relocated. If so, the relocation offset needs to be
1742 subtracted from these values when performing the comparison.
1743 We unconditionally subtract it, because, when no relocation
1744 has been performed, the value will simply be zero.
1746 The address of the symbol whose section we're fixing up HAS
1747 NOT BEEN adjusted (relocated) yet. It can't have been since
1748 the section isn't yet known and knowing the section is
1749 necessary in order to add the correct relocation value. In
1750 other words, we wouldn't even be in this function (attempting
1751 to compute the section) if it were already known.
1753 Note that it is possible to search the minimal symbols
1754 (subtracting the relocation value if necessary) to find the
1755 matching minimal symbol, but this is overkill and much less
1756 efficient. It is not necessary to find the matching minimal
1757 symbol, only its section.
1759 Note that this technique (of doing a section table search)
1760 can fail when unrelocated section addresses overlap. For
1761 this reason, we still attempt a lookup by name prior to doing
1762 a search of the section table. */
1764 for (obj_section
&s
: objfile
->sections ())
1766 if ((bfd_section_flags (s
.the_bfd_section
) & SEC_ALLOC
) == 0)
1769 int idx
= &s
- objfile
->sections_start
;
1770 CORE_ADDR offset
= objfile
->section_offsets
[idx
];
1775 if (s
.addr () - offset
<= addr
&& addr
< s
.endaddr () - offset
)
1777 sym
->set_section_index (idx
);
1782 /* If we didn't find the section, assume it is in the first
1783 section. If there is no allocated section, then it hardly
1784 matters what we pick, so just pick zero. */
1786 sym
->set_section_index (0);
1788 sym
->set_section_index (fallback
);
1794 demangle_for_lookup_info::demangle_for_lookup_info
1795 (const lookup_name_info
&lookup_name
, language lang
)
1797 demangle_result_storage storage
;
1799 if (lookup_name
.ignore_parameters () && lang
== language_cplus
)
1801 gdb::unique_xmalloc_ptr
<char> without_params
1802 = cp_remove_params_if_any (lookup_name
.c_str (),
1803 lookup_name
.completion_mode ());
1805 if (without_params
!= NULL
)
1807 if (lookup_name
.match_type () != symbol_name_match_type::SEARCH_NAME
)
1808 m_demangled_name
= demangle_for_lookup (without_params
.get (),
1814 if (lookup_name
.match_type () == symbol_name_match_type::SEARCH_NAME
)
1815 m_demangled_name
= lookup_name
.c_str ();
1817 m_demangled_name
= demangle_for_lookup (lookup_name
.c_str (),
1823 const lookup_name_info
&
1824 lookup_name_info::match_any ()
1826 /* Lookup any symbol that "" would complete. I.e., this matches all
1828 static const lookup_name_info
lookup_name ("", symbol_name_match_type::WILD
,
1837 lookup_name_info::search_name_hash (language lang
) const
1839 /* This works around an obscure problem. If currently in Ada mode,
1840 and the name is wrapped in '<...>' (indicating verbatim mode),
1841 force the use of the Ada language here so that the '<' and '>'
1843 if (current_language
->la_language
== language_ada
&& ada ().verbatim_p ())
1844 lang
= language_ada
;
1846 /* Only compute each language's hash once. */
1847 if (!m_demangled_hashes_p
[lang
])
1849 m_demangled_hashes
[lang
]
1850 = ::search_name_hash (lang
, language_lookup_name (lang
));
1851 m_demangled_hashes_p
[lang
] = true;
1853 return m_demangled_hashes
[lang
];
1856 /* Compute the demangled form of NAME as used by the various symbol
1857 lookup functions. The result can either be the input NAME
1858 directly, or a pointer to a buffer owned by the STORAGE object.
1860 For Ada, this function just returns NAME, unmodified.
1861 Normally, Ada symbol lookups are performed using the encoded name
1862 rather than the demangled name, and so it might seem to make sense
1863 for this function to return an encoded version of NAME.
1864 Unfortunately, we cannot do this, because this function is used in
1865 circumstances where it is not appropriate to try to encode NAME.
1866 For instance, when displaying the frame info, we demangle the name
1867 of each parameter, and then perform a symbol lookup inside our
1868 function using that demangled name. In Ada, certain functions
1869 have internally-generated parameters whose name contain uppercase
1870 characters. Encoding those name would result in those uppercase
1871 characters to become lowercase, and thus cause the symbol lookup
1875 demangle_for_lookup (const char *name
, enum language lang
,
1876 demangle_result_storage
&storage
)
1878 /* If we are using C++, D, or Go, demangle the name before doing a
1879 lookup, so we can always binary search. */
1880 if (lang
== language_cplus
)
1882 gdb::unique_xmalloc_ptr
<char> demangled_name
1883 = gdb_demangle (name
, DMGL_ANSI
| DMGL_PARAMS
);
1884 if (demangled_name
!= NULL
)
1885 return storage
.set_malloc_ptr (std::move (demangled_name
));
1887 /* If we were given a non-mangled name, canonicalize it
1888 according to the language (so far only for C++). */
1889 gdb::unique_xmalloc_ptr
<char> canon
= cp_canonicalize_string (name
);
1890 if (canon
!= nullptr)
1891 return storage
.set_malloc_ptr (std::move (canon
));
1893 else if (lang
== language_d
)
1895 gdb::unique_xmalloc_ptr
<char> demangled_name
= d_demangle (name
, 0);
1896 if (demangled_name
!= NULL
)
1897 return storage
.set_malloc_ptr (std::move (demangled_name
));
1899 else if (lang
== language_go
)
1901 gdb::unique_xmalloc_ptr
<char> demangled_name
1902 = language_def (language_go
)->demangle_symbol (name
, 0);
1903 if (demangled_name
!= NULL
)
1904 return storage
.set_malloc_ptr (std::move (demangled_name
));
1913 search_name_hash (enum language language
, const char *search_name
)
1915 return language_def (language
)->search_name_hash (search_name
);
1920 This function (or rather its subordinates) have a bunch of loops and
1921 it would seem to be attractive to put in some QUIT's (though I'm not really
1922 sure whether it can run long enough to be really important). But there
1923 are a few calls for which it would appear to be bad news to quit
1924 out of here: e.g., find_proc_desc in alpha-mdebug-tdep.c. (Note
1925 that there is C++ code below which can error(), but that probably
1926 doesn't affect these calls since they are looking for a known
1927 variable and thus can probably assume it will never hit the C++
1931 lookup_symbol_in_language (const char *name
, const struct block
*block
,
1932 const domain_search_flags domain
,
1934 struct field_of_this_result
*is_a_field_of_this
)
1936 SYMBOL_LOOKUP_SCOPED_DEBUG_ENTER_EXIT
;
1938 demangle_result_storage storage
;
1939 const char *modified_name
= demangle_for_lookup (name
, lang
, storage
);
1941 return lookup_symbol_aux (modified_name
,
1942 symbol_name_match_type::FULL
,
1943 block
, domain
, lang
,
1944 is_a_field_of_this
);
1950 lookup_symbol (const char *name
, const struct block
*block
,
1951 domain_search_flags domain
,
1952 struct field_of_this_result
*is_a_field_of_this
)
1954 return lookup_symbol_in_language (name
, block
, domain
,
1955 current_language
->la_language
,
1956 is_a_field_of_this
);
1962 lookup_symbol_search_name (const char *search_name
, const struct block
*block
,
1963 domain_search_flags domain
)
1965 return lookup_symbol_aux (search_name
, symbol_name_match_type::SEARCH_NAME
,
1966 block
, domain
, language_asm
, NULL
);
1972 lookup_language_this (const struct language_defn
*lang
,
1973 const struct block
*block
)
1975 if (lang
->name_of_this () == NULL
|| block
== NULL
)
1978 symbol_lookup_debug_printf_v ("lookup_language_this (%s, %s (objfile %s))",
1979 lang
->name (), host_address_to_string (block
),
1980 objfile_debug_name (block
->objfile ()));
1982 lookup_name_info
this_name (lang
->name_of_this (),
1983 symbol_name_match_type::SEARCH_NAME
);
1989 sym
= block_lookup_symbol (block
, this_name
, SEARCH_VFT
);
1992 symbol_lookup_debug_printf_v
1993 ("lookup_language_this (...) = %s (%s, block %s)",
1994 sym
->print_name (), host_address_to_string (sym
),
1995 host_address_to_string (block
));
1996 return (struct block_symbol
) {sym
, block
};
1998 if (block
->function ())
2000 block
= block
->superblock ();
2003 symbol_lookup_debug_printf_v ("lookup_language_this (...) = NULL");
2007 /* Given TYPE, a structure/union,
2008 return 1 if the component named NAME from the ultimate target
2009 structure/union is defined, otherwise, return 0. */
2012 check_field (struct type
*type
, const char *name
,
2013 struct field_of_this_result
*is_a_field_of_this
)
2017 /* The type may be a stub. */
2018 type
= check_typedef (type
);
2020 for (i
= type
->num_fields () - 1; i
>= TYPE_N_BASECLASSES (type
); i
--)
2022 const char *t_field_name
= type
->field (i
).name ();
2024 if (t_field_name
&& (strcmp_iw (t_field_name
, name
) == 0))
2026 is_a_field_of_this
->type
= type
;
2027 is_a_field_of_this
->field
= &type
->field (i
);
2032 /* C++: If it was not found as a data field, then try to return it
2033 as a pointer to a method. */
2035 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; --i
)
2037 if (strcmp_iw (TYPE_FN_FIELDLIST_NAME (type
, i
), name
) == 0)
2039 is_a_field_of_this
->type
= type
;
2040 is_a_field_of_this
->fn_field
= &TYPE_FN_FIELDLIST (type
, i
);
2045 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
2046 if (check_field (TYPE_BASECLASS (type
, i
), name
, is_a_field_of_this
))
2052 /* Behave like lookup_symbol except that NAME is the natural name
2053 (e.g., demangled name) of the symbol that we're looking for. */
2055 static struct block_symbol
2056 lookup_symbol_aux (const char *name
, symbol_name_match_type match_type
,
2057 const struct block
*block
,
2058 const domain_search_flags domain
, enum language language
,
2059 struct field_of_this_result
*is_a_field_of_this
)
2061 SYMBOL_LOOKUP_SCOPED_DEBUG_ENTER_EXIT
;
2063 struct block_symbol result
;
2064 const struct language_defn
*langdef
;
2066 if (symbol_lookup_debug
)
2068 struct objfile
*objfile
= (block
== nullptr
2069 ? nullptr : block
->objfile ());
2071 symbol_lookup_debug_printf
2072 ("demangled symbol name = \"%s\", block @ %s (objfile %s)",
2073 name
, host_address_to_string (block
),
2074 objfile
!= NULL
? objfile_debug_name (objfile
) : "NULL");
2075 symbol_lookup_debug_printf
2076 ("domain name = \"%s\", language = \"%s\")",
2077 domain_name (domain
).c_str (), language_str (language
));
2080 langdef
= language_def (language
);
2082 /* Search specified block and its superiors. Don't search
2083 STATIC_BLOCK or GLOBAL_BLOCK. */
2085 result
= lookup_local_symbol (name
, match_type
, block
, domain
, langdef
);
2086 if (result
.symbol
!= NULL
)
2088 symbol_lookup_debug_printf
2089 ("found symbol @ %s (using lookup_local_symbol)",
2090 host_address_to_string (result
.symbol
));
2094 /* If requested to do so by the caller and if appropriate for LANGUAGE,
2095 check to see if NAME is a field of `this'. */
2097 /* Don't do this check if we are searching for a struct. It will
2098 not be found by check_field, but will be found by other
2100 if (is_a_field_of_this
!= NULL
&& (domain
& SEARCH_STRUCT_DOMAIN
) == 0)
2102 result
= lookup_language_this (langdef
, block
);
2106 struct type
*t
= result
.symbol
->type ();
2108 /* I'm not really sure that type of this can ever
2109 be typedefed; just be safe. */
2110 t
= check_typedef (t
);
2111 if (t
->is_pointer_or_reference ())
2112 t
= t
->target_type ();
2114 if (t
->code () != TYPE_CODE_STRUCT
2115 && t
->code () != TYPE_CODE_UNION
)
2116 error (_("Internal error: `%s' is not an aggregate"),
2117 langdef
->name_of_this ());
2119 if (check_field (t
, name
, is_a_field_of_this
))
2121 symbol_lookup_debug_printf ("no symbol found");
2127 /* Now do whatever is appropriate for LANGUAGE to look
2128 up static and global variables. */
2130 result
= langdef
->lookup_symbol_nonlocal (name
, block
, domain
);
2131 if (result
.symbol
!= NULL
)
2133 symbol_lookup_debug_printf
2134 ("found symbol @ %s (using language lookup_symbol_nonlocal)",
2135 host_address_to_string (result
.symbol
));
2139 /* Now search all static file-level symbols. Not strictly correct,
2140 but more useful than an error. */
2142 result
= lookup_static_symbol (name
, domain
);
2143 symbol_lookup_debug_printf
2144 ("found symbol @ %s (using lookup_static_symbol)",
2145 result
.symbol
!= NULL
? host_address_to_string (result
.symbol
) : "NULL");
2149 /* Check to see if the symbol is defined in BLOCK or its superiors.
2150 Don't search STATIC_BLOCK or GLOBAL_BLOCK. */
2152 static struct block_symbol
2153 lookup_local_symbol (const char *name
,
2154 symbol_name_match_type match_type
,
2155 const struct block
*block
,
2156 const domain_search_flags domain
,
2157 const struct language_defn
*langdef
)
2159 if (block
== nullptr)
2162 const char *scope
= block
->scope ();
2164 while (!block
->is_global_block () && !block
->is_static_block ())
2166 struct symbol
*sym
= lookup_symbol_in_block (name
, match_type
,
2169 return (struct block_symbol
) {sym
, block
};
2171 struct symbol
*function
= block
->function ();
2172 if (function
!= nullptr && function
->is_template_function ())
2174 struct template_symbol
*templ
= (struct template_symbol
*) function
;
2175 sym
= search_symbol_list (name
,
2176 templ
->n_template_arguments
,
2177 templ
->template_arguments
);
2179 return (struct block_symbol
) {sym
, block
};
2182 struct block_symbol blocksym
2183 = langdef
->lookup_symbol_local (scope
, name
, block
, domain
);
2184 if (blocksym
.symbol
!= nullptr)
2187 if (block
->inlined_p ())
2189 block
= block
->superblock ();
2192 /* We've reached the end of the function without finding a result. */
2200 lookup_symbol_in_block (const char *name
, symbol_name_match_type match_type
,
2201 const struct block
*block
,
2202 const domain_search_flags domain
)
2206 if (symbol_lookup_debug
)
2208 struct objfile
*objfile
2209 = block
== nullptr ? nullptr : block
->objfile ();
2211 symbol_lookup_debug_printf_v
2212 ("lookup_symbol_in_block (%s, %s (objfile %s), %s)",
2213 name
, host_address_to_string (block
),
2214 objfile
!= nullptr ? objfile_debug_name (objfile
) : "NULL",
2215 domain_name (domain
).c_str ());
2218 lookup_name_info
lookup_name (name
, match_type
);
2219 sym
= block_lookup_symbol (block
, lookup_name
, domain
);
2222 symbol_lookup_debug_printf_v ("lookup_symbol_in_block (...) = %s",
2223 host_address_to_string (sym
));
2227 symbol_lookup_debug_printf_v ("lookup_symbol_in_block (...) = NULL");
2234 lookup_global_symbol_from_objfile (struct objfile
*main_objfile
,
2235 enum block_enum block_index
,
2237 const domain_search_flags domain
)
2239 gdb_assert (block_index
== GLOBAL_BLOCK
|| block_index
== STATIC_BLOCK
);
2241 for (objfile
*objfile
: main_objfile
->separate_debug_objfiles ())
2243 struct block_symbol result
2244 = lookup_symbol_in_objfile (objfile
, block_index
, name
, domain
);
2246 if (result
.symbol
!= nullptr)
2253 /* Check to see if the symbol is defined in one of the OBJFILE's
2254 symtabs. BLOCK_INDEX should be either GLOBAL_BLOCK or STATIC_BLOCK,
2255 depending on whether or not we want to search global symbols or
2258 static struct block_symbol
2259 lookup_symbol_in_objfile_symtabs (struct objfile
*objfile
,
2260 enum block_enum block_index
, const char *name
,
2261 const domain_search_flags domain
)
2263 gdb_assert (block_index
== GLOBAL_BLOCK
|| block_index
== STATIC_BLOCK
);
2265 symbol_lookup_debug_printf_v
2266 ("lookup_symbol_in_objfile_symtabs (%s, %s, %s, %s)",
2267 objfile_debug_name (objfile
),
2268 block_index
== GLOBAL_BLOCK
? "GLOBAL_BLOCK" : "STATIC_BLOCK",
2269 name
, domain_name (domain
).c_str ());
2271 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
2272 best_symbol_tracker accum
;
2273 for (compunit_symtab
&cust
: objfile
->compunits ())
2275 const struct blockvector
*bv
;
2276 const struct block
*block
;
2278 bv
= cust
.blockvector ();
2279 block
= bv
->block (block_index
);
2280 if (accum
.search (&cust
, block
, lookup_name
, domain
))
2284 if (accum
.currently_best
.symbol
!= nullptr)
2286 symbol_lookup_debug_printf_v
2287 ("lookup_symbol_in_objfile_symtabs (...) = %s (block %s)",
2288 host_address_to_string (accum
.currently_best
.symbol
),
2289 host_address_to_string (accum
.currently_best
.block
));
2290 return accum
.currently_best
;
2293 symbol_lookup_debug_printf_v
2294 ("lookup_symbol_in_objfile_symtabs (...) = NULL");
2298 /* Wrapper around lookup_symbol_in_objfile_symtabs for search_symbols.
2299 Look up LINKAGE_NAME in DOMAIN in the global and static blocks of OBJFILE
2300 and all associated separate debug objfiles.
2302 Normally we only look in OBJFILE, and not any separate debug objfiles
2303 because the outer loop will cause them to be searched too. This case is
2304 different. Here we're called from search_symbols where it will only
2305 call us for the objfile that contains a matching minsym. */
2307 static struct block_symbol
2308 lookup_symbol_in_objfile_from_linkage_name (struct objfile
*objfile
,
2309 const char *linkage_name
,
2310 domain_search_flags domain
)
2312 enum language lang
= current_language
->la_language
;
2313 struct objfile
*main_objfile
;
2315 demangle_result_storage storage
;
2316 const char *modified_name
= demangle_for_lookup (linkage_name
, lang
, storage
);
2318 if (objfile
->separate_debug_objfile_backlink
)
2319 main_objfile
= objfile
->separate_debug_objfile_backlink
;
2321 main_objfile
= objfile
;
2323 for (::objfile
*cur_objfile
: main_objfile
->separate_debug_objfiles ())
2325 struct block_symbol result
;
2327 result
= lookup_symbol_in_objfile_symtabs (cur_objfile
, GLOBAL_BLOCK
,
2328 modified_name
, domain
);
2329 if (result
.symbol
== NULL
)
2330 result
= lookup_symbol_in_objfile_symtabs (cur_objfile
, STATIC_BLOCK
,
2331 modified_name
, domain
);
2332 if (result
.symbol
!= NULL
)
2339 /* A helper function that throws an exception when a symbol was found
2340 in a psymtab but not in a symtab. */
2342 [[noreturn]] static void
2343 error_in_psymtab_expansion (enum block_enum block_index
, const char *name
,
2344 struct compunit_symtab
*cust
)
2347 Internal: %s symbol `%s' found in %s psymtab but not in symtab.\n\
2348 %s may be an inlined function, or may be a template function\n \
2349 (if a template, try specifying an instantiation: %s<type>)."),
2350 block_index
== GLOBAL_BLOCK
? "global" : "static",
2352 symtab_to_filename_for_display (cust
->primary_filetab ()),
2356 /* A helper function for various lookup routines that interfaces with
2357 the "quick" symbol table functions. */
2359 static struct block_symbol
2360 lookup_symbol_via_quick_fns (struct objfile
*objfile
,
2361 enum block_enum block_index
, const char *name
,
2362 const domain_search_flags domain
)
2364 symbol_lookup_debug_printf_v
2365 ("lookup_symbol_via_quick_fns (%s, %s, %s, %s)",
2366 objfile_debug_name (objfile
),
2367 block_index
== GLOBAL_BLOCK
? "GLOBAL_BLOCK" : "STATIC_BLOCK",
2368 name
, domain_name (domain
).c_str ());
2370 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
2371 best_symbol_tracker accum
;
2372 auto searcher
= [&] (compunit_symtab
*symtab
)
2374 const struct blockvector
*bv
= symtab
->blockvector ();
2375 const struct block
*block
= bv
->block (block_index
);
2376 /* If the accumulator finds a best symbol, end the search by
2377 returning false; otherwise keep going by returning true. */
2378 return !accum
.search (symtab
, block
, lookup_name
, domain
);
2381 objfile
->search (nullptr, &lookup_name
, nullptr, searcher
,
2382 block_index
== GLOBAL_BLOCK
2383 ? SEARCH_GLOBAL_BLOCK
2384 : SEARCH_STATIC_BLOCK
,
2386 if (accum
.best_symtab
== nullptr)
2388 symbol_lookup_debug_printf_v
2389 ("lookup_symbol_via_quick_fns (...) = NULL");
2392 if (accum
.currently_best
.symbol
== nullptr)
2393 error_in_psymtab_expansion (block_index
, name
, accum
.best_symtab
);
2395 symbol_lookup_debug_printf_v
2396 ("lookup_symbol_via_quick_fns (...) = %s (block %s)",
2397 host_address_to_string (accum
.currently_best
.symbol
),
2398 host_address_to_string (accum
.currently_best
.block
));
2400 return accum
.currently_best
;
2403 /* See language.h. */
2406 language_defn::lookup_symbol_nonlocal (const char *name
,
2407 const struct block
*block
,
2408 const domain_search_flags domain
) const
2410 struct block_symbol result
;
2412 /* NOTE: dje/2014-10-26: The lookup in all objfiles search could skip
2413 the current objfile. Searching the current objfile first is useful
2414 for both matching user expectations as well as performance. */
2416 result
= lookup_symbol_in_static_block (name
, block
, domain
);
2417 if (result
.symbol
!= NULL
)
2420 /* If we didn't find a definition for a builtin type in the static block,
2421 search for it now. This is actually the right thing to do and can be
2422 a massive performance win. E.g., when debugging a program with lots of
2423 shared libraries we could search all of them only to find out the
2424 builtin type isn't defined in any of them. This is common for types
2426 if ((domain
& SEARCH_TYPE_DOMAIN
) != 0)
2428 struct gdbarch
*gdbarch
;
2431 gdbarch
= current_inferior ()->arch ();
2433 gdbarch
= block
->gdbarch ();
2434 result
.symbol
= language_lookup_primitive_type_as_symbol (this,
2436 result
.block
= NULL
;
2437 if (result
.symbol
!= NULL
)
2441 return lookup_global_symbol (name
, block
, domain
);
2447 lookup_symbol_in_static_block (const char *name
,
2448 const struct block
*block
,
2449 const domain_search_flags domain
)
2451 if (block
== nullptr)
2454 const struct block
*static_block
= block
->static_block ();
2457 if (static_block
== NULL
)
2460 if (symbol_lookup_debug
)
2462 struct objfile
*objfile
= (block
== nullptr
2463 ? nullptr : block
->objfile ());
2465 symbol_lookup_debug_printf
2466 ("lookup_symbol_in_static_block (%s, %s (objfile %s), %s)",
2467 name
, host_address_to_string (block
),
2468 objfile
!= nullptr ? objfile_debug_name (objfile
) : "NULL",
2469 domain_name (domain
).c_str ());
2472 sym
= lookup_symbol_in_block (name
,
2473 symbol_name_match_type::FULL
,
2474 static_block
, domain
);
2475 symbol_lookup_debug_printf ("lookup_symbol_in_static_block (...) = %s",
2477 ? host_address_to_string (sym
) : "NULL");
2478 return (struct block_symbol
) {sym
, static_block
};
2481 /* Perform the standard symbol lookup of NAME in OBJFILE:
2482 1) First search expanded symtabs, and if not found
2483 2) Search the "quick" symtabs (partial or .gdb_index).
2484 BLOCK_INDEX is one of GLOBAL_BLOCK or STATIC_BLOCK. */
2486 static struct block_symbol
2487 lookup_symbol_in_objfile (struct objfile
*objfile
, enum block_enum block_index
,
2488 const char *name
, const domain_search_flags domain
)
2490 struct block_symbol result
;
2492 gdb_assert (block_index
== GLOBAL_BLOCK
|| block_index
== STATIC_BLOCK
);
2494 symbol_lookup_debug_printf ("lookup_symbol_in_objfile (%s, %s, %s, %s)",
2495 objfile_debug_name (objfile
),
2496 block_index
== GLOBAL_BLOCK
2497 ? "GLOBAL_BLOCK" : "STATIC_BLOCK",
2498 name
, domain_name (domain
).c_str ());
2500 result
= lookup_symbol_via_quick_fns (objfile
, block_index
,
2502 symbol_lookup_debug_printf ("lookup_symbol_in_objfile (...) = %s",
2503 result
.symbol
!= NULL
2504 ? host_address_to_string (result
.symbol
)
2509 /* This function contains the common code of lookup_{global,static}_symbol.
2510 OBJFILE is only used if BLOCK_INDEX is GLOBAL_SCOPE, in which case it is
2511 the objfile to start the lookup in. */
2513 static struct block_symbol
2514 lookup_global_or_static_symbol (const char *name
,
2515 enum block_enum block_index
,
2516 struct objfile
*objfile
,
2517 const domain_search_flags domain
)
2519 struct symbol_cache
*cache
= get_symbol_cache (current_program_space
);
2520 struct block_symbol result
;
2521 struct block_symbol_cache
*bsc
;
2522 struct symbol_cache_slot
*slot
;
2524 gdb_assert (block_index
== GLOBAL_BLOCK
|| block_index
== STATIC_BLOCK
);
2525 gdb_assert (objfile
== nullptr || block_index
== GLOBAL_BLOCK
);
2527 /* First see if we can find the symbol in the cache.
2528 This works because we use the current objfile to qualify the lookup. */
2529 result
= symbol_cache_lookup (cache
, objfile
, block_index
, name
, domain
,
2531 if (result
.symbol
!= NULL
)
2533 if (SYMBOL_LOOKUP_FAILED_P (result
))
2538 /* Do a global search (of global blocks, heh). */
2539 if (result
.symbol
== NULL
)
2540 current_program_space
->iterate_over_objfiles_in_search_order
2541 ([&result
, block_index
, name
, domain
] (struct objfile
*objfile_iter
)
2543 result
= lookup_symbol_in_objfile (objfile_iter
, block_index
,
2545 return result
.symbol
!= nullptr;
2549 if (result
.symbol
!= NULL
)
2550 symbol_cache_mark_found (bsc
, slot
, objfile
, result
.symbol
, result
.block
,
2553 symbol_cache_mark_not_found (bsc
, slot
, objfile
, name
, domain
);
2561 lookup_static_symbol (const char *name
, const domain_search_flags domain
)
2563 return lookup_global_or_static_symbol (name
, STATIC_BLOCK
, nullptr, domain
);
2569 lookup_global_symbol (const char *name
,
2570 const struct block
*block
,
2571 const domain_search_flags domain
)
2573 /* If a block was passed in, we want to search the corresponding
2574 global block first. This yields "more expected" behavior, and is
2575 needed to support 'FILENAME'::VARIABLE lookups. */
2576 const struct block
*global_block
2577 = block
== nullptr ? nullptr : block
->global_block ();
2579 if (global_block
!= nullptr)
2581 sym
= lookup_symbol_in_block (name
,
2582 symbol_name_match_type::FULL
,
2583 global_block
, domain
);
2584 if (sym
!= NULL
&& best_symbol (sym
, domain
))
2585 return { sym
, global_block
};
2588 struct objfile
*objfile
= nullptr;
2589 if (block
!= nullptr)
2591 objfile
= block
->objfile ();
2592 if (objfile
->separate_debug_objfile_backlink
!= nullptr)
2593 objfile
= objfile
->separate_debug_objfile_backlink
;
2597 = lookup_global_or_static_symbol (name
, GLOBAL_BLOCK
, objfile
, domain
);
2598 if (better_symbol (sym
, bs
.symbol
, domain
) == sym
)
2599 return { sym
, global_block
};
2607 symbol::matches (domain_search_flags flags
) const
2609 /* C++ has a typedef for every tag, and the types are in the struct
2611 if (language () == language_cplus
&& (flags
& SEARCH_TYPE_DOMAIN
) != 0)
2612 flags
|= SEARCH_STRUCT_DOMAIN
;
2614 return search_flags_matches (flags
, m_domain
);
2620 lookup_transparent_type (const char *name
, domain_search_flags flags
)
2622 return current_language
->lookup_transparent_type (name
, flags
);
2625 /* A helper for basic_lookup_transparent_type that interfaces with the
2626 "quick" symbol table functions. */
2628 static struct type
*
2629 basic_lookup_transparent_type_quick (struct objfile
*objfile
,
2630 enum block_enum block_index
,
2631 domain_search_flags flags
,
2632 const lookup_name_info
&name
)
2634 struct compunit_symtab
*cust
;
2635 const struct blockvector
*bv
;
2636 const struct block
*block
;
2639 cust
= objfile
->lookup_symbol (block_index
, name
, flags
);
2643 bv
= cust
->blockvector ();
2644 block
= bv
->block (block_index
);
2646 sym
= block_find_symbol (block
, name
, flags
, nullptr);
2648 error_in_psymtab_expansion (block_index
, name
.c_str (), cust
);
2649 gdb_assert (!TYPE_IS_OPAQUE (sym
->type ()));
2650 return sym
->type ();
2653 /* The standard implementation of lookup_transparent_type. This code
2654 was modeled on lookup_symbol -- the parts not relevant to looking
2655 up types were just left out. In particular it's assumed here that
2656 types are available in STRUCT_DOMAIN and only in file-static or
2660 basic_lookup_transparent_type (const char *name
, domain_search_flags flags
)
2664 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
2666 /* Search all the global symbols. */
2667 for (objfile
&objfile
: current_program_space
->objfiles ())
2669 t
= basic_lookup_transparent_type_quick (&objfile
, GLOBAL_BLOCK
,
2670 flags
, lookup_name
);
2675 /* Now search the static file-level symbols. Not strictly correct,
2676 but more useful than an error. */
2677 for (objfile
&objfile
: current_program_space
->objfiles ())
2679 t
= basic_lookup_transparent_type_quick (&objfile
, STATIC_BLOCK
,
2680 flags
, lookup_name
);
2685 return (struct type
*) 0;
2691 iterate_over_symbols (const struct block
*block
,
2692 const lookup_name_info
&name
,
2693 const domain_search_flags domain
,
2694 gdb::function_view
<symbol_found_callback_ftype
> callback
)
2696 for (struct symbol
*sym
: block_iterator_range (block
, &name
))
2698 if (sym
->matches (domain
))
2700 struct block_symbol block_sym
= {sym
, block
};
2702 if (!callback (&block_sym
))
2709 /* Find the compunit symtab associated with PC and SECTION.
2710 This will read in debug info as necessary. */
2712 struct compunit_symtab
*
2713 find_compunit_symtab_for_pc_sect (CORE_ADDR pc
, struct obj_section
*section
)
2715 struct compunit_symtab
*best_cust
= NULL
;
2716 CORE_ADDR best_cust_range
= 0;
2718 /* If we know that this is not a text address, return failure. This is
2719 necessary because we loop based on the block's high and low code
2720 addresses, which do not include the data ranges, and because
2721 we call find_pc_sect_psymtab which has a similar restriction based
2722 on the partial_symtab's texthigh and textlow. */
2723 bound_minimal_symbol msymbol
2724 = lookup_minimal_symbol_by_pc_section (pc
, section
);
2725 if (msymbol
.minsym
&& msymbol
.minsym
->data_p ())
2728 /* Search all symtabs for the one whose file contains our address, and which
2729 is the smallest of all the ones containing the address. This is designed
2730 to deal with a case like symtab a is at 0x1000-0x2000 and 0x3000-0x4000
2731 and symtab b is at 0x2000-0x3000. So the GLOBAL_BLOCK for a is from
2732 0x1000-0x4000, but for address 0x2345 we want to return symtab b.
2734 This happens for native ecoff format, where code from included files
2735 gets its own symtab. The symtab for the included file should have
2736 been read in already via the dependency mechanism.
2737 It might be swifter to create several symtabs with the same name
2738 like xcoff does (I'm not sure).
2740 It also happens for objfiles that have their functions reordered.
2741 For these, the symtab we are looking for is not necessarily read in. */
2743 for (objfile
&obj_file
: current_program_space
->objfiles ())
2745 for (compunit_symtab
&cust
: obj_file
.compunits ())
2747 const struct blockvector
*bv
= cust
.blockvector ();
2748 const struct block
*global_block
= bv
->global_block ();
2749 CORE_ADDR start
= global_block
->start ();
2750 CORE_ADDR end
= global_block
->end ();
2751 bool in_range_p
= start
<= pc
&& pc
< end
;
2755 if (bv
->map () != nullptr)
2757 if (bv
->map ()->find (pc
) == nullptr)
2763 CORE_ADDR range
= end
- start
;
2764 if (best_cust
!= nullptr
2765 && range
>= best_cust_range
)
2766 /* Cust doesn't have a smaller range than best_cust, skip it. */
2769 /* For an objfile that has its functions reordered,
2770 find_pc_psymtab will find the proper partial symbol table
2771 and we simply return its corresponding symtab. */
2772 /* In order to better support objfiles that contain both
2773 stabs and coff debugging info, we continue on if a psymtab
2775 struct compunit_symtab
*result
2776 = obj_file
.find_pc_sect_compunit_symtab (msymbol
, pc
,
2778 if (result
!= nullptr)
2783 struct symbol
*found_sym
= nullptr;
2785 for (int b_index
= GLOBAL_BLOCK
;
2786 b_index
<= STATIC_BLOCK
&& found_sym
== nullptr;
2789 const struct block
*b
= bv
->block (b_index
);
2790 for (struct symbol
*sym
: block_iterator_range (b
))
2792 if (matching_obj_sections (sym
->obj_section (&obj_file
),
2800 if (found_sym
== nullptr)
2801 continue; /* No symbol in this symtab matches
2805 /* Cust is best found so far, save it. */
2807 best_cust_range
= range
;
2811 if (best_cust
!= NULL
)
2814 /* Not found in symtabs, search the "quick" symtabs (e.g. psymtabs). */
2816 for (objfile
&objf
: current_program_space
->objfiles ())
2818 struct compunit_symtab
*result
2819 = objf
.find_pc_sect_compunit_symtab (msymbol
, pc
, section
, 1);
2827 /* Find the compunit symtab associated with PC.
2828 This will read in debug info as necessary.
2829 Backward compatibility, no section. */
2831 struct compunit_symtab
*
2832 find_compunit_symtab_for_pc (CORE_ADDR pc
)
2834 return find_compunit_symtab_for_pc_sect (pc
, find_pc_mapped_section (pc
));
2840 find_symbol_at_address (CORE_ADDR address
)
2842 for (objfile
&objfile
: current_program_space
->objfiles ())
2844 struct symbol
*sym
= objfile
.find_symbol_by_address (address
);
2854 /* Find the source file and line number for a given PC value and SECTION.
2855 Return a structure containing a symtab pointer, a line number,
2856 and a pc range for the entire source line.
2857 The value's .pc field is NOT the specified pc.
2858 NOTCURRENT nonzero means, if specified pc is on a line boundary,
2859 use the line that ends there. Otherwise, in that case, the line
2860 that begins there is used. */
2862 /* The big complication here is that a line may start in one file, and end just
2863 before the start of another file. This usually occurs when you #include
2864 code in the middle of a subroutine. To properly find the end of a line's PC
2865 range, we must search all symtabs associated with this compilation unit, and
2866 find the one whose first PC is closer than that of the next line in this
2869 struct symtab_and_line
2870 find_sal_for_pc_sect (CORE_ADDR pc
, struct obj_section
*section
, int notcurrent
)
2872 /* Info on best line seen so far, and where it starts, and its file. */
2873 const linetable_entry
*best
= NULL
;
2874 CORE_ADDR best_end
= 0;
2875 struct symtab
*best_symtab
= 0;
2877 if (section
== nullptr)
2879 section
= find_pc_overlay (pc
);
2880 if (section
== nullptr)
2881 section
= find_pc_section (pc
);
2884 /* Store here the first line number
2885 of a file which contains the line at the smallest pc after PC.
2886 If we don't find a line whose range contains PC,
2887 we will use a line one less than this,
2888 with a range from the start of that file to the first line's pc. */
2889 const linetable_entry
*alt
= NULL
;
2891 /* Info on best line seen in this file. */
2893 const linetable_entry
*prev
;
2895 /* If this pc is not from the current frame,
2896 it is the address of the end of a call instruction.
2897 Quite likely that is the start of the following statement.
2898 But what we want is the statement containing the instruction.
2899 Fudge the pc to make sure we get that. */
2901 /* It's tempting to assume that, if we can't find debugging info for
2902 any function enclosing PC, that we shouldn't search for line
2903 number info, either. However, GAS can emit line number info for
2904 assembly files --- very helpful when debugging hand-written
2905 assembly code. In such a case, we'd have no debug info for the
2906 function, but we would have line info. */
2911 /* elz: added this because this function returned the wrong
2912 information if the pc belongs to a stub (import/export)
2913 to call a shlib function. This stub would be anywhere between
2914 two functions in the target, and the line info was erroneously
2915 taken to be the one of the line before the pc. */
2917 /* RT: Further explanation:
2919 * We have stubs (trampolines) inserted between procedures.
2921 * Example: "shr1" exists in a shared library, and a "shr1" stub also
2922 * exists in the main image.
2924 * In the minimal symbol table, we have a bunch of symbols
2925 * sorted by start address. The stubs are marked as "trampoline",
2926 * the others appear as text. E.g.:
2928 * Minimal symbol table for main image
2929 * main: code for main (text symbol)
2930 * shr1: stub (trampoline symbol)
2931 * foo: code for foo (text symbol)
2933 * Minimal symbol table for "shr1" image:
2935 * shr1: code for shr1 (text symbol)
2938 * So the code below is trying to detect if we are in the stub
2939 * ("shr1" stub), and if so, find the real code ("shr1" trampoline),
2940 * and if found, do the symbolization from the real-code address
2941 * rather than the stub address.
2943 * Assumptions being made about the minimal symbol table:
2944 * 1. lookup_minimal_symbol_by_pc_section() will return a trampoline only
2945 * if we're really in the trampoline.s If we're beyond it (say
2946 * we're in "foo" in the above example), it'll have a closer
2947 * symbol (the "foo" text symbol for example) and will not
2948 * return the trampoline.
2949 * 2. lookup_minimal_symbol_text() will find a real text symbol
2950 * corresponding to the trampoline, and whose address will
2951 * be different than the trampoline address. I put in a sanity
2952 * check for the address being the same, to avoid an
2953 * infinite recursion.
2955 bound_minimal_symbol msymbol
= lookup_minimal_symbol_by_pc_section (pc
, section
);
2956 if (msymbol
.minsym
!= NULL
)
2957 if (msymbol
.minsym
->type () == mst_solib_trampoline
)
2959 bound_minimal_symbol mfunsym
2960 = lookup_minimal_symbol_text (current_program_space
,
2961 msymbol
.minsym
->linkage_name (),
2964 if (mfunsym
.minsym
== NULL
)
2965 /* I eliminated this warning since it is coming out
2966 * in the following situation:
2967 * gdb shmain // test program with shared libraries
2968 * (gdb) break shr1 // function in shared lib
2969 * Warning: In stub for ...
2970 * In the above situation, the shared lib is not loaded yet,
2971 * so of course we can't find the real func/line info,
2972 * but the "break" still works, and the warning is annoying.
2973 * So I commented out the warning. RT */
2974 /* warning ("In stub for %s; unable to find real function/line info",
2975 msymbol->linkage_name ()); */
2978 else if (mfunsym
.value_address ()
2979 == msymbol
.value_address ())
2980 /* Avoid infinite recursion */
2981 /* See above comment about why warning is commented out. */
2982 /* warning ("In stub for %s; unable to find real function/line info",
2983 msymbol->linkage_name ()); */
2988 /* Detect an obvious case of infinite recursion. If this
2989 should occur, we'd like to know about it, so error out,
2991 if (mfunsym
.value_address () == pc
)
2992 internal_error (_("Infinite recursion detected in find_sal_for_pc_sect;"
2993 "please file a bug report"));
2995 return find_sal_for_pc (mfunsym
.value_address (), 0);
2999 symtab_and_line val
;
3000 val
.pspace
= current_program_space
;
3002 compunit_symtab
*cust
= find_compunit_symtab_for_pc_sect (pc
, section
);
3005 /* If no symbol information, return previous pc. */
3009 val
.section
= section
;
3013 const blockvector
*bv
= cust
->blockvector ();
3014 struct objfile
*objfile
= cust
->objfile ();
3016 /* Look at all the symtabs that share this blockvector.
3017 They all have the same apriori range, that we found was right;
3018 but they have different line tables. */
3020 for (symtab
*iter_s
: cust
->filetabs ())
3022 /* Find the best line in this symtab. */
3023 const linetable
*l
= iter_s
->linetable ();
3027 int len
= l
->nitems
;
3030 /* I think len can be zero if the symtab lacks line numbers
3031 (e.g. gcc -g1). (Either that or the LINETABLE is NULL;
3032 I'm not sure which, and maybe it depends on the symbol
3038 /* Get first line info. */
3039 const linetable_entry
*item
= l
->item
;
3041 /* Is this file's first line closer than the first lines of other files?
3042 If so, record this file, and its first line, as best alternate. */
3043 if (item
->pc (objfile
) > pc
3044 && (!alt
|| item
->unrelocated_pc () < alt
->unrelocated_pc ()))
3047 auto pc_compare
= [] (const unrelocated_addr
&comp_pc
,
3048 const struct linetable_entry
& lhs
)
3050 return comp_pc
< lhs
.unrelocated_pc ();
3053 const linetable_entry
*first
= item
;
3054 const linetable_entry
*last
= item
+ len
;
3055 item
= (std::upper_bound
3057 unrelocated_addr (pc
- objfile
->text_section_offset ()),
3061 prev
= item
- 1; /* Found a matching item. */
3062 /* At this point, prev is a line whose address is <= pc. However, we
3063 don't know if ITEM is pointing to the same statement or not. */
3064 while (item
!= last
&& prev
->line
== item
->line
&& !item
->is_stmt
)
3068 /* At this point, prev points at the line whose start addr is <= pc, and
3069 item points at the next statement. If we ran off the end of the linetable
3070 (pc >= start of the last line), then prev == item. If pc < start of
3071 the first line, prev will not be set. */
3073 /* Is this file's best line closer than the best in the other files?
3074 If so, record this file, and its best line, as best so far. Don't
3075 save prev if it represents the end of a function (i.e. line number
3076 0) instead of a real line. */
3078 if (prev
&& prev
->line
3079 && (!best
|| prev
->unrelocated_pc () > best
->unrelocated_pc ()))
3082 best_symtab
= iter_s
;
3084 /* If during the binary search we land on a non-statement entry,
3085 scan backward through entries at the same address to see if
3086 there is an entry marked as is-statement. In theory this
3087 duplication should have been removed from the line table
3088 during construction, this is just a double check. If the line
3089 table has had the duplication removed then this should be
3093 const linetable_entry
*tmp
= best
;
3095 && (tmp
- 1)->unrelocated_pc () == tmp
->unrelocated_pc ()
3096 && (tmp
- 1)->line
!= 0 && !tmp
->is_stmt
)
3102 /* Discard BEST_END if it's before the PC of the current BEST. */
3103 if (best_end
<= best
->pc (objfile
))
3107 /* If another line (denoted by ITEM) is in the linetable and its
3108 PC is after BEST's PC, but before the current BEST_END, then
3109 use ITEM's PC as the new best_end. */
3110 if (best
&& item
< last
3111 && item
->unrelocated_pc () > best
->unrelocated_pc ()
3112 && (best_end
== 0 || best_end
> item
->pc (objfile
)))
3113 best_end
= item
->pc (objfile
);
3118 /* If we didn't find any line number info, just return zeros.
3119 We used to return alt->line - 1 here, but that could be
3120 anywhere; if we don't have line number info for this PC,
3121 don't make some up. */
3124 else if (best
->line
== 0)
3126 /* If our best fit is in a range of PC's for which no line
3127 number info is available (line number is zero) then we didn't
3128 find any valid line information. */
3133 val
.is_stmt
= best
->is_stmt
;
3134 val
.symtab
= best_symtab
;
3135 val
.line
= best
->line
;
3136 val
.pc
= best
->pc (objfile
);
3137 if (best_end
&& (!alt
|| best_end
< alt
->pc (objfile
)))
3140 val
.end
= alt
->pc (objfile
);
3142 val
.end
= bv
->global_block ()->end ();
3144 val
.section
= section
;
3148 /* Backward compatibility (no section). */
3150 struct symtab_and_line
3151 find_sal_for_pc (CORE_ADDR pc
, int notcurrent
)
3153 struct obj_section
*section
;
3155 section
= find_pc_overlay (pc
);
3156 if (!pc_in_unmapped_range (pc
, section
))
3157 return find_sal_for_pc_sect (pc
, section
, notcurrent
);
3159 /* If the original PC was an unmapped address then we translate this to a
3160 mapped address in order to lookup the sal. However, as the user
3161 passed us an unmapped address it makes more sense to return a result
3162 that has the pc and end fields translated to unmapped addresses. */
3163 pc
= overlay_mapped_address (pc
, section
);
3164 symtab_and_line sal
= find_sal_for_pc_sect (pc
, section
, notcurrent
);
3165 sal
.pc
= overlay_unmapped_address (sal
.pc
, section
);
3166 sal
.end
= overlay_unmapped_address (sal
.end
, section
);
3170 /* Compare two symtab_and_line entries. Return true if both have
3171 the same line number and the same symtab pointer. That means we
3172 are dealing with two entries from the same line and from the same
3175 Return false otherwise. */
3178 sal_line_symtab_matches_p (const symtab_and_line
&sal1
,
3179 const symtab_and_line
&sal2
)
3181 return sal1
.line
== sal2
.line
&& sal1
.symtab
== sal2
.symtab
;
3186 std::optional
<CORE_ADDR
>
3187 find_line_range_start (CORE_ADDR pc
)
3189 struct symtab_and_line current_sal
= find_sal_for_pc (pc
, 0);
3191 if (current_sal
.line
== 0)
3194 struct symtab_and_line prev_sal
= find_sal_for_pc (current_sal
.pc
- 1, 0);
3196 /* If the previous entry is for a different line, that means we are already
3197 at the entry with the start PC for this line. */
3198 if (!sal_line_symtab_matches_p (prev_sal
, current_sal
))
3199 return current_sal
.pc
;
3201 /* Otherwise, keep looking for entries for the same line but with
3207 prev_pc
= prev_sal
.pc
;
3209 prev_sal
= find_sal_for_pc (prev_pc
- 1, 0);
3211 /* Did we notice a line change? If so, we are done searching. */
3212 if (!sal_line_symtab_matches_p (prev_sal
, current_sal
))
3222 find_symtab_for_pc (CORE_ADDR pc
)
3224 struct symtab_and_line sal
;
3226 /* This always passes zero for NOTCURRENT to find_sal_for_pc.
3227 There are currently no callers that ever pass non-zero. */
3228 sal
= find_sal_for_pc (pc
, 0);
3235 find_line_symtab (symtab
*sym_tab
, int line
, int *index
)
3237 int exact
= 0; /* Initialized here to avoid a compiler warning. */
3239 /* BEST_INDEX and BEST_LINETABLE identify the smallest linenumber > LINE
3243 const struct linetable
*best_linetable
;
3244 struct symtab
*best_symtab
;
3246 /* First try looking it up in the given symtab. */
3247 best_linetable
= sym_tab
->linetable ();
3248 best_symtab
= sym_tab
;
3249 best_index
= find_line_common (best_linetable
, line
, &exact
, 0);
3250 if (best_index
< 0 || !exact
)
3252 /* Didn't find an exact match. So we better keep looking for
3253 another symtab with the same name. In the case of xcoff,
3254 multiple csects for one source file (produced by IBM's FORTRAN
3255 compiler) produce multiple symtabs (this is unavoidable
3256 assuming csects can be at arbitrary places in memory and that
3257 the GLOBAL_BLOCK of a symtab has a begin and end address). */
3259 /* BEST is the smallest linenumber > LINE so far seen,
3260 or 0 if none has been seen so far.
3261 BEST_INDEX and BEST_LINETABLE identify the item for it. */
3264 if (best_index
>= 0)
3265 best
= best_linetable
->item
[best_index
].line
;
3269 for (objfile
&objfile
: current_program_space
->objfiles ())
3270 objfile
.expand_symtabs_with_fullname (symtab_to_fullname (sym_tab
));
3272 for (objfile
&objfile
: current_program_space
->objfiles ())
3274 for (compunit_symtab
&cu
: objfile
.compunits ())
3276 for (symtab
*s
: cu
.filetabs ())
3278 const struct linetable
*l
;
3281 if (FILENAME_CMP (sym_tab
->filename (), s
->filename ()) != 0)
3283 if (FILENAME_CMP (symtab_to_fullname (sym_tab
),
3284 symtab_to_fullname (s
)) != 0)
3286 l
= s
->linetable ();
3287 ind
= find_line_common (l
, line
, &exact
, 0);
3297 if (best
== 0 || l
->item
[ind
].line
< best
)
3299 best
= l
->item
[ind
].line
;
3314 *index
= best_index
;
3319 /* Given SYMTAB, returns all the PCs function in the symtab that
3320 exactly match LINE. Returns an empty vector if there are no exact
3321 matches, but updates BEST_ITEM in this case. */
3323 std::vector
<const linetable_entry
*>
3324 find_linetable_entries_for_symtab_line (struct symtab
*symtab
, int line
,
3325 const linetable_entry
**best_item
)
3328 std::vector
<const linetable_entry
*> result
;
3330 /* First, collect all the PCs that are at this line. */
3336 idx
= find_line_common (symtab
->linetable (), line
, &was_exact
,
3343 const linetable_entry
*item
= &symtab
->linetable ()->item
[idx
];
3345 if (*best_item
== NULL
3346 || (item
->line
< (*best_item
)->line
&& item
->is_stmt
))
3352 result
.push_back (&symtab
->linetable ()->item
[idx
]);
3360 /* Set the PC value for a given source file and line number and return true.
3361 Returns false for invalid line number (and sets the PC to 0).
3362 The source file is specified with a struct symtab. */
3365 find_pc_for_line (struct symtab
*symtab
, int line
, CORE_ADDR
*pc
)
3367 const struct linetable
*l
;
3374 symtab
= find_line_symtab (symtab
, line
, &ind
);
3377 l
= symtab
->linetable ();
3378 *pc
= l
->item
[ind
].pc (symtab
->compunit ()->objfile ());
3385 /* Find the range of pc values in a line.
3386 Store the starting pc of the line into *STARTPTR
3387 and the ending pc (start of next line) into *ENDPTR.
3388 Returns true to indicate success.
3389 Returns false if could not find the specified line. */
3392 find_pc_range_for_sal (struct symtab_and_line sal
, CORE_ADDR
*startptr
,
3395 CORE_ADDR startaddr
;
3396 struct symtab_and_line found_sal
;
3399 if (startaddr
== 0 && !find_pc_for_line (sal
.symtab
, sal
.line
, &startaddr
))
3402 /* This whole function is based on address. For example, if line 10 has
3403 two parts, one from 0x100 to 0x200 and one from 0x300 to 0x400, then
3404 "info line *0x123" should say the line goes from 0x100 to 0x200
3405 and "info line *0x355" should say the line goes from 0x300 to 0x400.
3406 This also insures that we never give a range like "starts at 0x134
3407 and ends at 0x12c". */
3409 found_sal
= find_sal_for_pc_sect (startaddr
, sal
.section
, 0);
3410 if (found_sal
.line
!= sal
.line
)
3412 /* The specified line (sal) has zero bytes. */
3413 *startptr
= found_sal
.pc
;
3414 *endptr
= found_sal
.pc
;
3418 *startptr
= found_sal
.pc
;
3419 *endptr
= found_sal
.end
;
3424 /* Given a line table and a line number, return the index into the line
3425 table for the pc of the nearest line whose number is >= the specified one.
3426 Return -1 if none is found. The value is >= 0 if it is an index.
3427 START is the index at which to start searching the line table.
3429 Set *EXACT_MATCH nonzero if the value returned is an exact match. */
3432 find_line_common (const linetable
*l
, int lineno
,
3433 int *exact_match
, int start
)
3438 /* BEST is the smallest linenumber > LINENO so far seen,
3439 or 0 if none has been seen so far.
3440 BEST_INDEX identifies the item for it. */
3442 int best_index
= -1;
3453 for (i
= start
; i
< len
; i
++)
3455 const linetable_entry
*item
= &(l
->item
[i
]);
3457 /* Ignore non-statements. */
3461 if (item
->line
== lineno
)
3463 /* Return the first (lowest address) entry which matches. */
3468 if (item
->line
> lineno
&& (best
== 0 || item
->line
< best
))
3475 /* If we got here, we didn't get an exact match. */
3480 find_line_pc_range_for_pc (CORE_ADDR pc
, CORE_ADDR
*startptr
, CORE_ADDR
*endptr
)
3482 struct symtab_and_line sal
;
3484 sal
= find_sal_for_pc (pc
, 0);
3487 return sal
.symtab
!= 0;
3490 /* Helper for find_function_start_sal. Does most of the work, except
3491 setting the sal's symbol. */
3493 static symtab_and_line
3494 find_function_start_sal_1 (CORE_ADDR func_addr
, obj_section
*section
,
3497 symtab_and_line sal
= find_sal_for_pc_sect (func_addr
, section
, 0);
3499 if (funfirstline
&& sal
.symtab
!= NULL
3500 && (sal
.symtab
->compunit ()->locations_valid ()
3501 || sal
.symtab
->language () == language_asm
))
3503 struct gdbarch
*gdbarch
= sal
.symtab
->compunit ()->objfile ()->arch ();
3506 if (gdbarch_skip_entrypoint_p (gdbarch
))
3507 sal
.pc
= gdbarch_skip_entrypoint (gdbarch
, sal
.pc
);
3511 /* We always should have a line for the function start address.
3512 If we don't, something is odd. Create a plain SAL referring
3513 just the PC and hope that skip_prologue_sal (if requested)
3514 can find a line number for after the prologue. */
3515 if (sal
.pc
< func_addr
)
3518 sal
.pspace
= current_program_space
;
3520 sal
.section
= section
;
3524 skip_prologue_sal (&sal
);
3532 find_function_start_sal (CORE_ADDR func_addr
, obj_section
*section
, bool funfirstline
)
3535 = find_function_start_sal_1 (func_addr
, section
, funfirstline
);
3537 /* find_function_start_sal_1 does a linetable search, so it finds
3538 the symtab and linenumber, but not a symbol. Fill in the
3539 function symbol too. */
3540 sal
.symbol
= find_symbol_for_pc_sect_maybe_inline (sal
.pc
, sal
.section
);
3548 find_function_start_sal (symbol
*sym
, bool funfirstline
)
3551 = find_function_start_sal_1 (sym
->value_block ()->entry_pc (),
3552 sym
->obj_section (sym
->objfile ()),
3559 /* Given a function start address FUNC_ADDR and SYMTAB, find the first
3560 address for that function that has an entry in SYMTAB's line info
3561 table. If such an entry cannot be found, return FUNC_ADDR
3565 skip_prologue_using_lineinfo (CORE_ADDR func_addr
, struct symtab
*symtab
)
3567 CORE_ADDR func_start
, func_end
;
3568 const struct linetable
*l
;
3571 /* Give up if this symbol has no lineinfo table. */
3572 l
= symtab
->linetable ();
3576 /* Get the range for the function's PC values, or give up if we
3577 cannot, for some reason. */
3578 if (!find_pc_partial_function (func_addr
, NULL
, &func_start
, &func_end
))
3581 struct objfile
*objfile
= symtab
->compunit ()->objfile ();
3583 /* Linetable entries are ordered by PC values, see the commentary in
3584 symtab.h where `struct linetable' is defined. Thus, the first
3585 entry whose PC is in the range [FUNC_START..FUNC_END[ is the
3586 address we are looking for. */
3587 for (i
= 0; i
< l
->nitems
; i
++)
3589 const linetable_entry
*item
= &(l
->item
[i
]);
3590 CORE_ADDR item_pc
= item
->pc (objfile
);
3592 /* Don't use line numbers of zero, they mark special entries in
3593 the table. See the commentary on symtab.h before the
3594 definition of struct linetable. */
3595 if (item
->line
> 0 && func_start
<= item_pc
&& item_pc
< func_end
)
3602 /* Try to locate the address where a breakpoint should be placed past the
3603 prologue of function starting at FUNC_ADDR using the line table.
3605 Return the address associated with the first entry in the line-table for
3606 the function starting at FUNC_ADDR which has prologue_end set to true if
3607 such entry exist, otherwise return an empty optional. */
3609 static std::optional
<CORE_ADDR
>
3610 skip_prologue_using_linetable (CORE_ADDR func_addr
)
3612 CORE_ADDR start_pc
, end_pc
;
3614 if (!find_pc_partial_function (func_addr
, nullptr, &start_pc
, &end_pc
))
3617 const struct symtab_and_line prologue_sal
= find_sal_for_pc (start_pc
, 0);
3618 if (prologue_sal
.symtab
!= nullptr
3619 && prologue_sal
.symtab
->language () != language_asm
)
3621 const linetable
*linetable
= prologue_sal
.symtab
->linetable ();
3623 struct objfile
*objfile
= prologue_sal
.symtab
->compunit ()->objfile ();
3625 unrelocated_addr unrel_start
3626 = unrelocated_addr (start_pc
- objfile
->text_section_offset ());
3627 unrelocated_addr unrel_end
3628 = unrelocated_addr (end_pc
- objfile
->text_section_offset ());
3630 auto it
= std::lower_bound
3631 (linetable
->item
, linetable
->item
+ linetable
->nitems
, unrel_start
,
3632 [] (const linetable_entry
<e
, unrelocated_addr pc
)
3634 return lte
.unrelocated_pc () < pc
;
3638 (it
< linetable
->item
+ linetable
->nitems
3639 && it
->unrelocated_pc () < unrel_end
);
3641 if (it
->prologue_end
)
3642 return {it
->pc (objfile
)};
3648 /* Adjust SAL to the first instruction past the function prologue.
3649 If the PC was explicitly specified, the SAL is not changed.
3650 If the line number was explicitly specified then the SAL can still be
3651 updated, unless the language for SAL is assembler, in which case the SAL
3652 will be left unchanged.
3653 If SAL is already past the prologue, then do nothing. */
3656 skip_prologue_sal (struct symtab_and_line
*sal
)
3658 /* Do not change the SAL if PC was specified explicitly. */
3659 if (sal
->explicit_pc
)
3662 /* In assembly code, if the user asks for a specific line then we should
3663 not adjust the SAL. The user already has instruction level
3664 visibility in this case, so selecting a line other than one requested
3665 is likely to be the wrong choice. */
3666 if (sal
->symtab
!= nullptr
3667 && sal
->explicit_line
3668 && sal
->symtab
->language () == language_asm
)
3671 scoped_restore_current_pspace_and_thread restore_pspace_thread
;
3673 switch_to_program_space_and_thread (sal
->pspace
);
3675 symbol
*sym
= find_symbol_for_pc_sect (sal
->pc
, sal
->section
);
3678 obj_section
*section
;
3683 objfile
= sym
->objfile ();
3684 pc
= sym
->value_block ()->entry_pc ();
3685 section
= sym
->obj_section (objfile
);
3686 name
= sym
->linkage_name ();
3690 bound_minimal_symbol msymbol
3691 = lookup_minimal_symbol_by_pc_section (sal
->pc
, sal
->section
);
3693 if (msymbol
.minsym
== NULL
)
3696 objfile
= msymbol
.objfile
;
3697 pc
= msymbol
.value_address ();
3698 section
= msymbol
.minsym
->obj_section (objfile
);
3699 name
= msymbol
.minsym
->linkage_name ();
3702 gdbarch
*gdbarch
= objfile
->arch ();
3704 /* Process the prologue in two passes. In the first pass try to skip the
3705 prologue (SKIP is true) and verify there is a real need for it (indicated
3706 by FORCE_SKIP). If no such reason was found run a second pass where the
3707 prologue is not skipped (SKIP is false). */
3712 /* Be conservative - allow direct PC (without skipping prologue) only if we
3713 have proven the CU (Compilation Unit) supports it. sal->SYMTAB does not
3714 have to be set by the caller so we use SYM instead. */
3716 && sym
->symtab ()->compunit ()->locations_valid ())
3719 symtab_and_line start_sal
;
3720 CORE_ADDR saved_pc
= pc
;
3726 /* Check if the compiler explicitly indicated where a breakpoint should
3727 be placed to skip the prologue. */
3728 if (!ignore_prologue_end_flag
&& skip
)
3730 std::optional
<CORE_ADDR
> linetable_pc
3731 = skip_prologue_using_linetable (pc
);
3735 start_sal
= find_sal_for_pc_sect (pc
, section
, 0);
3741 /* If the function is in an unmapped overlay, use its unmapped LMA address,
3742 so that gdbarch_skip_prologue has something unique to work on. */
3743 if (section_is_overlay (section
) && !section_is_mapped (section
))
3744 pc
= overlay_unmapped_address (pc
, section
);
3746 /* Skip "first line" of function (which is actually its prologue). */
3747 pc
+= gdbarch_deprecated_function_start_offset (gdbarch
);
3748 if (gdbarch_skip_entrypoint_p (gdbarch
))
3749 pc
= gdbarch_skip_entrypoint (gdbarch
, pc
);
3751 pc
= gdbarch_skip_prologue_noexcept (gdbarch
, pc
);
3753 /* For overlays, map pc back into its mapped VMA range. */
3754 pc
= overlay_mapped_address (pc
, section
);
3756 /* Calculate line number. */
3757 start_sal
= find_sal_for_pc_sect (pc
, section
, 0);
3759 /* Check if gdbarch_skip_prologue left us in mid-line, and the next
3760 line is still part of the same function. */
3761 if (skip
&& start_sal
.pc
!= pc
3762 && (sym
? (sym
->value_block ()->entry_pc () <= start_sal
.end
3763 && start_sal
.end
< sym
->value_block()->end ())
3764 : (lookup_minimal_symbol_by_pc_section (start_sal
.end
, section
).minsym
3765 == lookup_minimal_symbol_by_pc_section (pc
, section
).minsym
)))
3767 /* First pc of next line */
3769 /* Recalculate the line number (might not be N+1). */
3770 start_sal
= find_sal_for_pc_sect (pc
, section
, 0);
3773 /* On targets with executable formats that don't have a concept of
3774 constructors (ELF with .init has, PE doesn't), gcc emits a call
3775 to `__main' in `main' between the prologue and before user
3777 if (gdbarch_skip_main_prologue_p (gdbarch
)
3778 && name
&& strcmp_iw (name
, "main") == 0)
3780 pc
= gdbarch_skip_main_prologue (gdbarch
, pc
);
3781 /* Recalculate the line number (might not be N+1). */
3782 start_sal
= find_sal_for_pc_sect (pc
, section
, 0);
3786 while (!force_skip
&& skip
--);
3788 /* If we still don't have a valid source line, try to find the first
3789 PC in the lineinfo table that belongs to the same function. This
3790 happens with COFF debug info, which does not seem to have an
3791 entry in lineinfo table for the code after the prologue which has
3792 no direct relation to source. For example, this was found to be
3793 the case with the DJGPP target using "gcc -gcoff" when the
3794 compiler inserted code after the prologue to make sure the stack
3796 if (!force_skip
&& sym
&& start_sal
.symtab
== NULL
)
3798 pc
= skip_prologue_using_lineinfo (pc
, sym
->symtab ());
3799 /* Recalculate the line number. */
3800 start_sal
= find_sal_for_pc_sect (pc
, section
, 0);
3803 /* If we're already past the prologue, leave SAL unchanged. Otherwise
3804 forward SAL to the end of the prologue. */
3809 sal
->section
= section
;
3810 sal
->symtab
= start_sal
.symtab
;
3811 sal
->line
= start_sal
.line
;
3812 sal
->end
= start_sal
.end
;
3814 /* Check if we are now inside an inlined function. If we can,
3815 use the call site of the function instead. */
3816 const block
*function_block
= nullptr;
3818 for (const block
*b
= block_for_pc_sect (sal
->pc
, sal
->section
);
3820 b
= b
->superblock ())
3821 if (b
->function () != NULL
&& b
->inlined_p ())
3823 else if (b
->function () != NULL
)
3826 if (function_block
!= NULL
3827 && function_block
->function ()->line () != 0)
3829 sal
->line
= function_block
->function ()->line ();
3830 sal
->symtab
= function_block
->function ()->symtab ();
3834 /* Given PC at the function's start address, attempt to find the
3835 prologue end using SAL information. Return zero if the skip fails.
3837 A non-optimized prologue traditionally has one SAL for the function
3838 and a second for the function body. A single line function has
3839 them both pointing at the same line.
3841 An optimized prologue is similar but the prologue may contain
3842 instructions (SALs) from the instruction body. Need to skip those
3843 while not getting into the function body.
3845 The functions end point and an increasing SAL line are used as
3846 indicators of the prologue's endpoint.
3848 This code is based on the function refine_prologue_limit
3852 skip_prologue_using_sal (struct gdbarch
*gdbarch
, CORE_ADDR func_addr
)
3854 struct symtab_and_line prologue_sal
;
3857 const struct block
*bl
;
3859 /* Get an initial range for the function. */
3860 find_pc_partial_function (func_addr
, NULL
, &start_pc
, &end_pc
);
3861 start_pc
+= gdbarch_deprecated_function_start_offset (gdbarch
);
3863 prologue_sal
= find_sal_for_pc (start_pc
, 0);
3864 if (prologue_sal
.line
!= 0)
3866 /* For languages other than assembly, treat two consecutive line
3867 entries at the same address as a zero-instruction prologue.
3868 The GNU assembler emits separate line notes for each instruction
3869 in a multi-instruction macro, but compilers generally will not
3871 if (prologue_sal
.symtab
->language () != language_asm
)
3873 struct objfile
*objfile
3874 = prologue_sal
.symtab
->compunit ()->objfile ();
3875 const linetable
*linetable
= prologue_sal
.symtab
->linetable ();
3876 gdb_assert (linetable
->nitems
> 0);
3879 /* Skip any earlier lines, and any end-of-sequence marker
3880 from a previous function. */
3881 while (idx
+ 1 < linetable
->nitems
3882 && (linetable
->item
[idx
].pc (objfile
) != prologue_sal
.pc
3883 || linetable
->item
[idx
].line
== 0))
3886 if (idx
+ 1 < linetable
->nitems
3887 && linetable
->item
[idx
+1].line
!= 0
3888 && linetable
->item
[idx
+1].pc (objfile
) == start_pc
)
3892 /* If there is only one sal that covers the entire function,
3893 then it is probably a single line function, like
3895 if (prologue_sal
.end
>= end_pc
)
3898 while (prologue_sal
.end
< end_pc
)
3900 struct symtab_and_line sal
;
3902 sal
= find_sal_for_pc (prologue_sal
.end
, 0);
3905 /* Assume that a consecutive SAL for the same (or larger)
3906 line mark the prologue -> body transition. */
3907 if (sal
.line
>= prologue_sal
.line
)
3909 /* Likewise if we are in a different symtab altogether
3910 (e.g. within a file included via #include). */
3911 if (sal
.symtab
!= prologue_sal
.symtab
)
3914 /* The line number is smaller. Check that it's from the
3915 same function, not something inlined. If it's inlined,
3916 then there is no point comparing the line numbers. */
3917 bl
= block_for_pc (prologue_sal
.end
);
3920 if (bl
->inlined_p ())
3922 if (bl
->function ())
3927 bl
= bl
->superblock ();
3932 /* The case in which compiler's optimizer/scheduler has
3933 moved instructions into the prologue. We look ahead in
3934 the function looking for address ranges whose
3935 corresponding line number is less the first one that we
3936 found for the function. This is more conservative then
3937 refine_prologue_limit which scans a large number of SALs
3938 looking for any in the prologue. */
3943 if (prologue_sal
.end
< end_pc
)
3944 /* Return the end of this line, or zero if we could not find a
3946 return prologue_sal
.end
;
3948 /* Don't return END_PC, which is past the end of the function. */
3949 return prologue_sal
.pc
;
3954 std::optional
<CORE_ADDR
>
3955 find_epilogue_using_linetable (CORE_ADDR func_addr
)
3957 CORE_ADDR start_pc
, end_pc
;
3959 if (!find_pc_partial_function (func_addr
, nullptr, &start_pc
, &end_pc
))
3962 /* While the standard allows for multiple points marked with epilogue_begin
3963 in the same function, for performance reasons, this function will only
3964 find the last address that sets this flag for a given block.
3966 The lines of a function can be described by several line tables in case
3967 there are different files involved. There's a corner case where a
3968 function epilogue is in a different file than a function start, and using
3969 start_pc as argument to find_sal_for_pc will mean we won't find the
3970 epilogue. Instead, use "end_pc - 1" to maximize our chances of picking
3971 the line table containing an epilogue. */
3972 const struct symtab_and_line sal
= find_sal_for_pc (end_pc
- 1, 0);
3973 if (sal
.symtab
!= nullptr && sal
.symtab
->language () != language_asm
)
3975 struct objfile
*objfile
= sal
.symtab
->compunit ()->objfile ();
3976 unrelocated_addr unrel_start
3977 = unrelocated_addr (start_pc
- objfile
->text_section_offset ());
3978 unrelocated_addr unrel_end
3979 = unrelocated_addr (end_pc
- objfile
->text_section_offset ());
3981 const linetable
*linetable
= sal
.symtab
->linetable ();
3982 if (linetable
== nullptr || linetable
->nitems
== 0)
3984 /* Empty line table. */
3988 /* Find the first linetable entry after the current function. Note that
3989 this also may be an end_sequence entry. */
3990 auto it
= std::lower_bound
3991 (linetable
->item
, linetable
->item
+ linetable
->nitems
, unrel_end
,
3992 [] (const linetable_entry
<e
, unrelocated_addr pc
)
3994 return lte
.unrelocated_pc () < pc
;
3996 if (it
== linetable
->item
+ linetable
->nitems
)
3998 /* We couldn't find either:
3999 - a linetable entry starting the function after the current
4001 - an end_sequence entry that terminates the current function
4004 This can happen when the linetable doesn't describe the full
4005 extent of the function. This can be triggered with:
4006 - compiler-generated debug info, in the cornercase that the pc
4007 with which we call find_sal_for_pc resides in a different file
4009 - invalid dwarf assembly debug info.
4010 In the former case, there's no point in iterating further, simply
4011 return "not found". In the latter case, there's no current
4012 incentive to attempt to support this, so handle this
4013 conservatively and do the same. */
4017 if (unrel_end
< it
->unrelocated_pc ())
4019 /* We found a line entry that starts past the end of the
4020 function. This can happen if the previous entry straddles
4021 two functions, which shouldn't happen with compiler-generated
4022 debug info. Handle the corner case conservatively. */
4025 gdb_assert (unrel_end
== it
->unrelocated_pc ());
4027 /* Move to the last linetable entry of the current function. */
4028 if (it
== &linetable
->item
[0])
4030 /* Doing it-- would introduce undefined behavior, avoid it by
4031 explicitly handling this case. */
4035 if (it
->unrelocated_pc () < unrel_start
)
4037 /* Not in the current function. */
4040 gdb_assert (it
->unrelocated_pc () < unrel_end
);
4042 /* We're at the the last linetable entry of the current function. This
4043 is probably where the epilogue begins, but since the DWARF 5 spec
4044 doesn't guarantee it, we iterate backwards through the current
4045 function until we either find the epilogue beginning, or are sure
4046 that it doesn't exist. */
4047 for (; it
>= &linetable
->item
[0]; it
--)
4049 if (it
->unrelocated_pc () < unrel_start
)
4051 /* No longer in the current function. */
4055 if (it
->epilogue_begin
)
4057 /* Found the beginning of the epilogue. */
4058 return {it
->pc (objfile
)};
4061 if (it
== &linetable
->item
[0])
4063 /* No more entries in the current function.
4064 Doing it-- would introduce undefined behavior, avoid it by
4065 explicitly handling this case. */
4077 find_function_alias_target (bound_minimal_symbol msymbol
)
4079 CORE_ADDR func_addr
;
4080 if (!msymbol_is_function (msymbol
.objfile
, msymbol
.minsym
, &func_addr
))
4083 symbol
*sym
= find_symbol_for_pc (func_addr
);
4085 && sym
->loc_class () == LOC_BLOCK
4086 && sym
->value_block ()->entry_pc () == func_addr
)
4093 /* If P is of the form "operator[ \t]+..." where `...' is
4094 some legitimate operator text, return a pointer to the
4095 beginning of the substring of the operator text.
4096 Otherwise, return "". */
4099 operator_chars (const char *p
, const char **end
)
4102 if (!startswith (p
, CP_OPERATOR_STR
))
4104 p
+= CP_OPERATOR_LEN
;
4106 /* Don't get faked out by `operator' being part of a longer
4108 if (c_isalpha (*p
) || *p
== '_' || *p
== '$' || *p
== '\0')
4111 /* Allow some whitespace between `operator' and the operator symbol. */
4112 while (*p
== ' ' || *p
== '\t')
4115 /* Recognize 'operator TYPENAME'. */
4117 if (c_isalpha (*p
) || *p
== '_' || *p
== '$')
4119 const char *q
= p
+ 1;
4121 while (c_isalnum (*q
) || *q
== '_' || *q
== '$')
4130 case '\\': /* regexp quoting */
4133 if (p
[2] == '=') /* 'operator\*=' */
4135 else /* 'operator\*' */
4139 else if (p
[1] == '[')
4142 error (_("mismatched quoting on brackets, "
4143 "try 'operator\\[\\]'"));
4144 else if (p
[2] == '\\' && p
[3] == ']')
4146 *end
= p
+ 4; /* 'operator\[\]' */
4150 error (_("nothing is allowed between '[' and ']'"));
4154 /* Gratuitous quote: skip it and move on. */
4176 if (p
[0] == '-' && p
[1] == '>')
4178 /* Struct pointer member operator 'operator->'. */
4181 *end
= p
+ 3; /* 'operator->*' */
4184 else if (p
[2] == '\\')
4186 *end
= p
+ 4; /* Hopefully 'operator->\*' */
4191 *end
= p
+ 2; /* 'operator->' */
4195 if (p
[1] == '=' || p
[1] == p
[0])
4206 error (_("`operator ()' must be specified "
4207 "without whitespace in `()'"));
4212 error (_("`operator ?:' must be specified "
4213 "without whitespace in `?:'"));
4218 error (_("`operator []' must be specified "
4219 "without whitespace in `[]'"));
4223 error (_("`operator %s' not supported"), p
);
4232 /* See class declaration. */
4234 info_sources_filter::info_sources_filter (match_on match_type
,
4236 : m_match_type (match_type
),
4239 /* Setup the compiled regular expression M_C_REGEXP based on M_REGEXP. */
4240 if (m_regexp
!= nullptr && *m_regexp
!= '\0')
4242 gdb_assert (m_regexp
!= nullptr);
4244 int cflags
= REG_NOSUB
;
4245 #ifdef HAVE_CASE_INSENSITIVE_FILE_SYSTEM
4246 cflags
|= REG_ICASE
;
4248 m_c_regexp
.emplace (m_regexp
, cflags
, _("Invalid regexp"));
4252 /* See class declaration. */
4255 info_sources_filter::matches (const char *fullname
) const
4257 /* Does it match regexp? */
4258 if (m_c_regexp
.has_value ())
4260 const char *to_match
;
4261 std::string dirname
;
4263 switch (m_match_type
)
4265 case match_on::DIRNAME
:
4266 dirname
= gdb_ldirname (fullname
);
4267 to_match
= dirname
.c_str ();
4269 case match_on::BASENAME
:
4270 to_match
= lbasename (fullname
);
4272 case match_on::FULLNAME
:
4273 to_match
= fullname
;
4276 gdb_assert_not_reached ("bad m_match_type");
4279 if (m_c_regexp
->exec (to_match
, 0, NULL
, 0) != 0)
4286 /* Data structure to maintain the state used for printing the results of
4287 the 'info sources' command. */
4289 struct output_source_filename_data
4291 /* Create an object for displaying the results of the 'info sources'
4292 command to UIOUT. FILTER must remain valid and unchanged for the
4293 lifetime of this object as this object retains a reference to FILTER. */
4294 output_source_filename_data (struct ui_out
*uiout
,
4295 const info_sources_filter
&filter
)
4296 : m_filter (filter
),
4300 DISABLE_COPY_AND_ASSIGN (output_source_filename_data
);
4302 /* Reset enough state of this object so we can match against a new set of
4303 files. The existing regular expression is retained though. */
4304 void reset_output ()
4307 m_filename_seen_cache
.clear ();
4310 /* Worker for sources_info, outputs the file name formatted for either
4311 cli or mi (based on the current_uiout). In cli mode displays
4312 FULLNAME with a comma separating this name from any previously
4313 printed name (line breaks are added at the comma). In MI mode
4314 outputs a tuple containing DISP_NAME (the files display name),
4315 FULLNAME, and EXPANDED_P (true when this file is from a fully
4316 expanded symtab, otherwise false). */
4317 void output (const char *disp_name
, const char *fullname
, bool expanded_p
);
4319 /* An overload suitable for use as a callback to
4320 quick_symbol_functions::map_symbol_filenames. */
4321 void operator() (const char *filename
, const char *fullname
)
4323 /* The false here indicates that this file is from an unexpanded
4325 output (filename
, fullname
, false);
4328 /* Return true if at least one filename has been printed (after a call to
4329 output) since either this object was created, or the last call to
4331 bool printed_filename_p () const
4338 /* Flag of whether we're printing the first one. */
4339 bool m_first
= true;
4341 /* Cache of what we've seen so far. */
4342 filename_seen_cache m_filename_seen_cache
;
4344 /* How source filename should be filtered. */
4345 const info_sources_filter
&m_filter
;
4347 /* The object to which output is sent. */
4348 struct ui_out
*m_uiout
;
4351 /* See comment in class declaration above. */
4354 output_source_filename_data::output (const char *disp_name
,
4355 const char *fullname
,
4358 /* Since a single source file can result in several partial symbol
4359 tables, we need to avoid printing it more than once. Note: if
4360 some of the psymtabs are read in and some are not, it gets
4361 printed both under "Source files for which symbols have been
4362 read" and "Source files for which symbols will be read in on
4363 demand". I consider this a reasonable way to deal with the
4364 situation. I'm not sure whether this can also happen for
4365 symtabs; it doesn't hurt to check. */
4367 /* Was NAME already seen? If so, then don't print it again. */
4368 if (m_filename_seen_cache
.seen (fullname
))
4371 /* If the filter rejects this file then don't print it. */
4372 if (!m_filter
.matches (fullname
))
4375 ui_out_emit_tuple
ui_emitter (m_uiout
, nullptr);
4377 /* Print it and reset *FIRST. */
4379 m_uiout
->text (", ");
4382 m_uiout
->wrap_hint (0);
4383 if (m_uiout
->is_mi_like_p ())
4385 m_uiout
->field_string ("file", disp_name
, file_name_style
.style ());
4386 if (fullname
!= nullptr)
4387 m_uiout
->field_string ("fullname", fullname
,
4388 file_name_style
.style ());
4389 m_uiout
->field_string ("debug-fully-read",
4390 (expanded_p
? "true" : "false"));
4394 if (fullname
== nullptr)
4395 fullname
= disp_name
;
4396 m_uiout
->field_string ("fullname", fullname
,
4397 file_name_style
.style ());
4401 /* For the 'info sources' command, what part of the file names should we be
4402 matching the user supplied regular expression against? */
4404 struct filename_partial_match_opts
4406 /* Only match the directory name part. */
4407 bool dirname
= false;
4409 /* Only match the basename part. */
4410 bool basename
= false;
4413 using isrc_flag_option_def
4414 = gdb::option::flag_option_def
<filename_partial_match_opts
>;
4416 static const gdb::option::option_def info_sources_option_defs
[] = {
4418 isrc_flag_option_def
{
4420 [] (filename_partial_match_opts
*opts
) { return &opts
->dirname
; },
4421 N_("Show only the files having a dirname matching REGEXP."),
4424 isrc_flag_option_def
{
4426 [] (filename_partial_match_opts
*opts
) { return &opts
->basename
; },
4427 N_("Show only the files having a basename matching REGEXP."),
4432 /* Create an option_def_group for the "info sources" options, with
4433 ISRC_OPTS as context. */
4435 static inline gdb::option::option_def_group
4436 make_info_sources_options_def_group (filename_partial_match_opts
*isrc_opts
)
4438 return {{info_sources_option_defs
}, isrc_opts
};
4441 /* Completer for "info sources". */
4444 info_sources_command_completer (cmd_list_element
*ignore
,
4445 completion_tracker
&tracker
,
4446 const char *text
, const char *word
)
4448 const auto group
= make_info_sources_options_def_group (nullptr);
4449 if (gdb::option::complete_options
4450 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
4457 info_sources_worker (struct ui_out
*uiout
,
4458 bool group_by_objfile
,
4459 const info_sources_filter
&filter
)
4461 output_source_filename_data
data (uiout
, filter
);
4463 ui_out_emit_list
results_emitter (uiout
, "files");
4464 std::optional
<ui_out_emit_tuple
> output_tuple
;
4465 std::optional
<ui_out_emit_list
> sources_list
;
4467 gdb_assert (group_by_objfile
|| uiout
->is_mi_like_p ());
4469 for (objfile
&objfile
: current_program_space
->objfiles ())
4471 if (group_by_objfile
)
4473 output_tuple
.emplace (uiout
, nullptr);
4474 uiout
->field_string ("filename", objfile_name (&objfile
),
4475 file_name_style
.style ());
4476 uiout
->text (":\n");
4477 bool debug_fully_readin
= !objfile
.has_unexpanded_symtabs ();
4478 if (uiout
->is_mi_like_p ())
4480 const char *debug_info_state
;
4481 if (objfile
.has_symbols ())
4483 if (debug_fully_readin
)
4484 debug_info_state
= "fully-read";
4486 debug_info_state
= "partially-read";
4489 debug_info_state
= "none";
4490 current_uiout
->field_string ("debug-info", debug_info_state
);
4494 if (!debug_fully_readin
)
4495 uiout
->text ("(Full debug information has not yet been read "
4496 "for this file.)\n");
4497 if (!objfile
.has_symbols ())
4498 uiout
->text ("(Objfile has no debug information.)\n");
4501 sources_list
.emplace (uiout
, "sources");
4504 for (compunit_symtab
&cu
: objfile
.compunits ())
4506 for (symtab
*s
: cu
.filetabs ())
4508 const char *file
= symtab_to_filename_for_display (s
);
4509 const char *fullname
= symtab_to_fullname (s
);
4510 data
.output (file
, fullname
, true);
4514 if (group_by_objfile
)
4516 objfile
.map_symbol_filenames (data
, true /* need_fullname */);
4517 if (data
.printed_filename_p ())
4518 uiout
->text ("\n\n");
4519 data
.reset_output ();
4520 sources_list
.reset ();
4521 output_tuple
.reset ();
4525 if (!group_by_objfile
)
4527 data
.reset_output ();
4528 current_program_space
->map_symbol_filenames (data
,
4529 true /*need_fullname*/);
4533 /* Implement the 'info sources' command. */
4536 info_sources_command (const char *args
, int from_tty
)
4538 if (!have_full_symbols (current_program_space
)
4539 && !have_partial_symbols (current_program_space
))
4540 error (_("No symbol table is loaded. Use the \"file\" command."));
4542 filename_partial_match_opts match_opts
;
4543 auto group
= make_info_sources_options_def_group (&match_opts
);
4544 gdb::option::process_options
4545 (&args
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, group
);
4547 if (match_opts
.dirname
&& match_opts
.basename
)
4548 error (_("You cannot give both -basename and -dirname to 'info sources'."));
4550 const char *regex
= nullptr;
4551 if (args
!= NULL
&& *args
!= '\000')
4554 if ((match_opts
.dirname
|| match_opts
.basename
) && regex
== nullptr)
4555 error (_("Missing REGEXP for 'info sources'."));
4557 info_sources_filter::match_on match_type
;
4558 if (match_opts
.dirname
)
4559 match_type
= info_sources_filter::match_on::DIRNAME
;
4560 else if (match_opts
.basename
)
4561 match_type
= info_sources_filter::match_on::BASENAME
;
4563 match_type
= info_sources_filter::match_on::FULLNAME
;
4565 info_sources_filter
filter (match_type
, regex
);
4566 info_sources_worker (current_uiout
, true, filter
);
4569 /* Compare FILE against all the entries of FILENAMES. If BASENAMES is
4570 true compare only lbasename of FILENAMES. */
4573 file_matches (const char *file
,
4574 const std::vector
<gdb::unique_xmalloc_ptr
<char>> &filenames
,
4577 if (filenames
.empty ())
4580 for (const auto &name
: filenames
)
4582 const char *lname
= (basenames
? lbasename (name
.get ()) : name
.get ());
4583 if (compare_filenames_for_search (file
, lname
))
4590 /* Helper function for std::sort on symbol_search objects. Can only sort
4591 symbols, not minimal symbols. */
4594 symbol_search::compare_search_syms (const symbol_search
&sym_a
,
4595 const symbol_search
&sym_b
)
4599 c
= FILENAME_CMP (sym_a
.symbol
->symtab ()->filename (),
4600 sym_b
.symbol
->symtab ()->filename ());
4604 if (sym_a
.block
!= sym_b
.block
)
4605 return sym_a
.block
- sym_b
.block
;
4607 c
= strcmp (sym_a
.symbol
->print_name (), sym_b
.symbol
->print_name ());
4612 /* These two symbols have the same name. It is possible, with types,
4613 that we can see two symbols with the same name, but different types,
4614 consider in C: 'typedef struct foo { ... } foo;' which creates a
4615 'struct foo' type and a 'foo' typedef type. For now this is the only
4616 case we handle. In all other cases, we treat symbols with the same
4617 name as being the same.
4620 First, check the types, if they are the same, then consider these
4621 symbols as the same. */
4622 if (sym_a
.symbol
->type ()->code () == sym_b
.symbol
->type ()->code ())
4625 /* The types are different, but if neither is a typedef then we still
4626 consider these symbols as the same. */
4627 if (sym_a
.symbol
->type ()->code () != TYPE_CODE_TYPEDEF
4628 && sym_b
.symbol
->type ()->code () != TYPE_CODE_TYPEDEF
)
4631 /* The symbols have different types, and one is a typedef. They cannot
4632 both be typedefs or we'd have taken the "types are the same" exit path
4633 above. If the two types are defined on different lines then order by
4634 line number. As line numbers are unsigned, don't subtract one from
4635 the other in order to avoid underflow. */
4636 if (sym_a
.symbol
->line () != sym_b
.symbol
->line ())
4637 return (sym_a
.symbol
->line () > sym_b
.symbol
->line () ? 1 : -1);
4639 /* The symbols have different types, and one is a typedef, but both
4640 symbols are defined on the same line. For example:
4642 typedef struct foo { int a; } foo;
4644 In this case we sort the typedef after the non-typedef. This is an
4645 arbitrary decision, but I think looks slightly nicer in the 'info
4646 types' output; first we get the type, then the typedef. */
4647 if (sym_a
.symbol
->type ()->code () == TYPE_CODE_TYPEDEF
)
4653 /* Returns true if the type_name of symbol_type of SYM matches TREG.
4654 If SYM has no symbol_type or symbol_name, returns false. */
4657 treg_matches_sym_type_name (const compiled_regex
&treg
,
4658 const struct symbol
*sym
)
4660 struct type
*sym_type
;
4661 std::string printed_sym_type_name
;
4663 symbol_lookup_debug_printf_v ("treg_matches_sym_type_name, sym %s",
4664 sym
->natural_name ());
4666 sym_type
= sym
->type ();
4667 if (sym_type
== NULL
)
4671 scoped_switch_to_sym_language_if_auto
l (sym
);
4673 printed_sym_type_name
= type_to_string (sym_type
);
4676 symbol_lookup_debug_printf_v ("sym_type_name %s",
4677 printed_sym_type_name
.c_str ());
4679 if (printed_sym_type_name
.empty ())
4682 return treg
.exec (printed_sym_type_name
.c_str (), 0, NULL
, 0) == 0;
4688 global_symbol_searcher::is_suitable_msymbol
4689 (const domain_search_flags kind
, const minimal_symbol
*msymbol
)
4691 switch (msymbol
->type ())
4697 return (kind
& SEARCH_VAR_DOMAIN
) != 0;
4700 case mst_solib_trampoline
:
4701 case mst_text_gnu_ifunc
:
4702 return (kind
& SEARCH_FUNCTION_DOMAIN
) != 0;
4711 global_symbol_searcher::expand_symtabs
4712 (objfile
*objfile
, const std::optional
<compiled_regex
> &preg
) const
4714 domain_search_flags kind
= m_kind
;
4715 bool found_msymbol
= false;
4717 auto do_file_match
= [&] (const char *filename
, bool basenames
)
4719 return file_matches (filename
, m_filenames
, basenames
);
4721 search_symtabs_file_matcher file_matcher
= nullptr;
4722 if (!m_filenames
.empty ())
4723 file_matcher
= do_file_match
;
4727 &lookup_name_info::match_any (),
4728 [&] (const char *symname
)
4730 return (!preg
.has_value ()
4731 || preg
->exec (symname
, 0, NULL
, 0) == 0);
4734 SEARCH_GLOBAL_BLOCK
| SEARCH_STATIC_BLOCK
,
4737 /* Here, we search through the minimal symbol tables for functions and
4738 variables that match, and force their symbols to be read. This is in
4739 particular necessary for demangled variable names, which are no longer
4740 put into the partial symbol tables. The symbol will then be found
4741 during the scan of symtabs later.
4743 For functions, find_pc_symtab should succeed if we have debug info for
4744 the function, for variables we have to call
4745 lookup_symbol_in_objfile_from_linkage_name to determine if the
4746 variable has debug info. If the lookup fails, set found_msymbol so
4747 that we will rescan to print any matching symbols without debug info.
4748 We only search the objfile the msymbol came from, we no longer search
4749 all objfiles. In large programs (1000s of shared libs) searching all
4750 objfiles is not worth the pain. */
4751 if (m_filenames
.empty ()
4752 && (kind
& (SEARCH_VAR_DOMAIN
| SEARCH_FUNCTION_DOMAIN
)) != 0)
4754 for (minimal_symbol
*msymbol
: objfile
->msymbols ())
4758 if (msymbol
->created_by_gdb
)
4761 if (is_suitable_msymbol (kind
, msymbol
))
4763 if (!preg
.has_value ()
4764 || preg
->exec (msymbol
->natural_name (), 0,
4767 /* An important side-effect of these lookup functions is
4768 to expand the symbol table if msymbol is found, later
4769 in the process we will add matching symbols or
4770 msymbols to the results list, and that requires that
4771 the symbols tables are expanded. */
4772 if ((kind
& SEARCH_FUNCTION_DOMAIN
) != 0
4773 ? (find_compunit_symtab_for_pc
4774 (msymbol
->value_address (objfile
)) == NULL
)
4775 : (lookup_symbol_in_objfile_from_linkage_name
4776 (objfile
, msymbol
->linkage_name (),
4779 found_msymbol
= true;
4785 return found_msymbol
;
4791 global_symbol_searcher::add_matching_symbols
4793 const std::optional
<compiled_regex
> &preg
,
4794 const std::optional
<compiled_regex
> &treg
,
4795 std::set
<symbol_search
> *result_set
) const
4797 domain_search_flags kind
= m_kind
;
4799 /* Add matching symbols (if not already present). */
4800 for (compunit_symtab
&cust
: objfile
->compunits ())
4802 const struct blockvector
*bv
= cust
.blockvector ();
4804 for (block_enum block
: { GLOBAL_BLOCK
, STATIC_BLOCK
})
4806 const struct block
*b
= bv
->block (block
);
4808 for (struct symbol
*sym
: block_iterator_range (b
))
4810 struct symtab
*real_symtab
= sym
->symtab ();
4814 /* Check first sole REAL_SYMTAB->FILENAME. It does
4815 not need to be a substring of symtab_to_fullname as
4816 it may contain "./" etc. */
4817 if (!(file_matches (real_symtab
->filename (), m_filenames
, false)
4818 || ((basenames_may_differ
4819 || file_matches (lbasename (real_symtab
->filename ()),
4821 && file_matches (symtab_to_fullname (real_symtab
),
4822 m_filenames
, false))))
4825 if (!sym
->matches (kind
))
4828 if (preg
.has_value () && preg
->exec (sym
->natural_name (), 0,
4832 if (((sym
->domain () == VAR_DOMAIN
4833 || sym
->domain () == FUNCTION_DOMAIN
)
4834 && treg
.has_value ()
4835 && !treg_matches_sym_type_name (*treg
, sym
)))
4838 if ((kind
& SEARCH_VAR_DOMAIN
) != 0)
4840 if (sym
->loc_class () == LOC_UNRESOLVED
4841 /* LOC_CONST can be used for more than
4842 just enums, e.g., c++ static const
4843 members. We only want to skip enums
4845 || (sym
->loc_class () == LOC_CONST
4846 && (sym
->type ()->code () == TYPE_CODE_ENUM
)))
4849 if (sym
->domain () == MODULE_DOMAIN
&& sym
->line () == 0)
4852 if (result_set
->size () < m_max_search_results
)
4854 /* Match, insert if not already in the results. */
4855 symbol_search
ss (block
, sym
);
4856 if (result_set
->find (ss
) == result_set
->end ())
4857 result_set
->insert (ss
);
4871 global_symbol_searcher::add_matching_msymbols
4872 (objfile
*objfile
, const std::optional
<compiled_regex
> &preg
,
4873 std::vector
<symbol_search
> *results
) const
4875 domain_search_flags kind
= m_kind
;
4877 for (minimal_symbol
*msymbol
: objfile
->msymbols ())
4881 if (msymbol
->created_by_gdb
)
4884 if (is_suitable_msymbol (kind
, msymbol
))
4886 if (!preg
.has_value ()
4887 || preg
->exec (msymbol
->natural_name (), 0,
4890 /* For functions we can do a quick check of whether the
4891 symbol might be found via find_pc_symtab. */
4892 if ((kind
& SEARCH_FUNCTION_DOMAIN
) == 0
4893 || (find_compunit_symtab_for_pc
4894 (msymbol
->value_address (objfile
)) == NULL
))
4896 if (lookup_symbol_in_objfile_from_linkage_name
4897 (objfile
, msymbol
->linkage_name (),
4898 SEARCH_VFT
).symbol
== NULL
)
4900 /* Matching msymbol, add it to the results list. */
4901 if (results
->size () < m_max_search_results
)
4902 results
->emplace_back (GLOBAL_BLOCK
, msymbol
, objfile
);
4916 std::vector
<symbol_search
>
4917 global_symbol_searcher::search () const
4919 std::optional
<compiled_regex
> preg
;
4920 std::optional
<compiled_regex
> treg
;
4922 if (m_symbol_name_regexp
!= NULL
)
4924 const char *symbol_name_regexp
= m_symbol_name_regexp
;
4925 std::string symbol_name_regexp_holder
;
4927 /* Make sure spacing is right for C++ operators.
4928 This is just a courtesy to make the matching less sensitive
4929 to how many spaces the user leaves between 'operator'
4930 and <TYPENAME> or <OPERATOR>. */
4932 const char *opname
= operator_chars (symbol_name_regexp
, &opend
);
4936 int fix
= -1; /* -1 means ok; otherwise number of
4939 if (c_isalpha (*opname
) || *opname
== '_' || *opname
== '$')
4941 /* There should 1 space between 'operator' and 'TYPENAME'. */
4942 if (opname
[-1] != ' ' || opname
[-2] == ' ')
4947 /* There should 0 spaces between 'operator' and 'OPERATOR'. */
4948 if (opname
[-1] == ' ')
4951 /* If wrong number of spaces, fix it. */
4954 symbol_name_regexp_holder
4955 = string_printf ("operator%.*s%s", fix
, " ", opname
);
4956 symbol_name_regexp
= symbol_name_regexp_holder
.c_str ();
4960 int cflags
= REG_NOSUB
| (case_sensitivity
== case_sensitive_off
4962 preg
.emplace (symbol_name_regexp
, cflags
,
4963 _("Invalid regexp"));
4966 if (m_symbol_type_regexp
!= NULL
)
4968 int cflags
= REG_NOSUB
| (case_sensitivity
== case_sensitive_off
4970 treg
.emplace (m_symbol_type_regexp
, cflags
,
4971 _("Invalid regexp"));
4974 bool found_msymbol
= false;
4975 std::set
<symbol_search
> result_set
;
4976 for (objfile
&objfile
: current_program_space
->objfiles ())
4978 /* Expand symtabs within objfile that possibly contain matching
4980 found_msymbol
|= expand_symtabs (&objfile
, preg
);
4982 /* Find matching symbols within OBJFILE and add them in to the
4983 RESULT_SET set. Use a set here so that we can easily detect
4984 duplicates as we go, and can therefore track how many unique
4985 matches we have found so far. */
4986 if (!add_matching_symbols (&objfile
, preg
, treg
, &result_set
))
4990 /* Convert the result set into a sorted result list, as std::set is
4991 defined to be sorted then no explicit call to std::sort is needed. */
4992 std::vector
<symbol_search
> result (result_set
.begin (), result_set
.end ());
4994 /* If there are no debug symbols, then add matching minsyms. But if the
4995 user wants to see symbols matching a type regexp, then never give a
4996 minimal symbol, as we assume that a minimal symbol does not have a
4999 || (m_filenames
.empty () && (m_kind
& SEARCH_VAR_DOMAIN
) != 0))
5000 && !m_exclude_minsyms
5001 && !treg
.has_value ())
5003 gdb_assert ((m_kind
& (SEARCH_VAR_DOMAIN
| SEARCH_FUNCTION_DOMAIN
))
5005 for (objfile
&objfile
: current_program_space
->objfiles ())
5006 if (!add_matching_msymbols (&objfile
, preg
, &result
))
5016 symbol_to_info_string (struct symbol
*sym
, int block
)
5020 gdb_assert (block
== GLOBAL_BLOCK
|| block
== STATIC_BLOCK
);
5022 if (block
== STATIC_BLOCK
5023 && (sym
->domain () == VAR_DOMAIN
5024 || sym
->domain () == FUNCTION_DOMAIN
))
5027 /* Typedef that is not a C++ class. */
5028 if (sym
->domain () == TYPE_DOMAIN
)
5030 string_file tmp_stream
;
5032 /* FIXME: For C (and C++) we end up with a difference in output here
5033 between how a typedef is printed, and non-typedefs are printed.
5034 The TYPEDEF_PRINT code places a ";" at the end in an attempt to
5035 appear C-like, while TYPE_PRINT doesn't.
5037 For the struct printing case below, things are worse, we force
5038 printing of the ";" in this function, which is going to be wrong
5039 for languages that don't require a ";" between statements. */
5040 if (sym
->type ()->code () == TYPE_CODE_TYPEDEF
)
5041 typedef_print (sym
->type (), sym
, &tmp_stream
);
5043 type_print (sym
->type (), "", &tmp_stream
, -1);
5044 str
+= tmp_stream
.string ();
5046 /* variable, func, or typedef-that-is-c++-class. */
5047 else if (sym
->domain () == VAR_DOMAIN
|| sym
->domain () == STRUCT_DOMAIN
5048 || sym
->domain () == FUNCTION_DOMAIN
)
5050 string_file tmp_stream
;
5052 type_print (sym
->type (),
5053 (sym
->loc_class () == LOC_TYPEDEF
5054 ? "" : sym
->print_name ()),
5057 str
+= tmp_stream
.string ();
5060 /* Printing of modules is currently done here, maybe at some future
5061 point we might want a language specific method to print the module
5062 symbol so that we can customise the output more. */
5063 else if (sym
->domain () == MODULE_DOMAIN
)
5064 str
+= sym
->print_name ();
5069 /* Helper function for symbol info commands, for example 'info
5070 functions', 'info variables', etc. BLOCK is the type of block the
5071 symbols was found in, either GLOBAL_BLOCK or STATIC_BLOCK. SYM is
5072 the symbol we found. If LAST is not NULL, print file and line
5073 number information for the symbol as well. Skip printing the
5074 filename if it matches LAST. */
5077 print_symbol_info (struct symbol
*sym
, int block
, const char *last
)
5079 scoped_switch_to_sym_language_if_auto
l (sym
);
5080 struct symtab
*s
= sym
->symtab ();
5084 const char *s_filename
= symtab_to_filename_for_display (s
);
5086 if (filename_cmp (last
, s_filename
) != 0)
5088 gdb_printf (_("\nFile %ps:\n"),
5089 styled_string (file_name_style
.style (),
5093 if (sym
->line () != 0)
5094 gdb_printf ("%d:\t", sym
->line ());
5099 std::string str
= symbol_to_info_string (sym
, block
);
5100 gdb_printf ("%s\n", str
.c_str ());
5103 /* This help function for symtab_symbol_info() prints information
5104 for non-debugging symbols to gdb_stdout. */
5107 print_msymbol_info (bound_minimal_symbol msymbol
)
5109 struct gdbarch
*gdbarch
= msymbol
.objfile
->arch ();
5112 if (gdbarch_addr_bit (gdbarch
) <= 32)
5113 tmp
= hex_string_custom (msymbol
.value_address ()
5114 & (CORE_ADDR
) 0xffffffff,
5117 tmp
= hex_string_custom (msymbol
.value_address (),
5120 ui_file_style sym_style
= (msymbol
.minsym
->text_p ()
5121 ? function_name_style
.style ()
5122 : ui_file_style ());
5124 gdb_printf (_("%ps %ps\n"),
5125 styled_string (address_style
.style (), tmp
),
5126 styled_string (sym_style
, msymbol
.minsym
->print_name ()));
5129 /* This is the guts of the commands "info functions", "info types", and
5130 "info variables". It calls search_symbols to find all matches and then
5131 print_[m]symbol_info to print out some useful information about the
5135 symtab_symbol_info (bool quiet
, bool exclude_minsyms
,
5136 const char *regexp
, domain_enum kind
,
5137 const char *t_regexp
, int from_tty
)
5139 const char *last_filename
= "";
5142 if (regexp
!= nullptr && *regexp
== '\0')
5145 domain_search_flags flags
= to_search_flags (kind
);
5146 if (kind
== TYPE_DOMAIN
)
5147 flags
|= SEARCH_STRUCT_DOMAIN
;
5149 global_symbol_searcher
spec (flags
, regexp
);
5150 spec
.set_symbol_type_regexp (t_regexp
);
5151 spec
.set_exclude_minsyms (exclude_minsyms
);
5152 std::vector
<symbol_search
> symbols
= spec
.search ();
5156 const char *classname
;
5160 classname
= "variable";
5162 case FUNCTION_DOMAIN
:
5163 classname
= "function";
5169 classname
= "module";
5172 gdb_assert_not_reached ("invalid domain enum");
5177 if (t_regexp
!= NULL
)
5179 (_("All %ss matching regular expression \"%s\""
5180 " with type matching regular expression \"%s\":\n"),
5181 classname
, regexp
, t_regexp
);
5183 gdb_printf (_("All %ss matching regular expression \"%s\":\n"),
5188 if (t_regexp
!= NULL
)
5190 (_("All defined %ss"
5191 " with type matching regular expression \"%s\" :\n"),
5192 classname
, t_regexp
);
5194 gdb_printf (_("All defined %ss:\n"), classname
);
5198 for (const symbol_search
&p
: symbols
)
5202 if (p
.msymbol
.minsym
!= NULL
)
5207 gdb_printf (_("\nNon-debugging symbols:\n"));
5210 print_msymbol_info (p
.msymbol
);
5214 print_symbol_info (p
.symbol
, p
.block
, last_filename
);
5216 = symtab_to_filename_for_display (p
.symbol
->symtab ());
5221 /* Structure to hold the values of the options used by the 'info variables'
5222 and 'info functions' commands. These correspond to the -q, -t, and -n
5225 struct info_vars_funcs_options
5228 bool exclude_minsyms
= false;
5229 std::string type_regexp
;
5232 /* The options used by the 'info variables' and 'info functions'
5235 static const gdb::option::option_def info_vars_funcs_options_defs
[] = {
5236 gdb::option::boolean_option_def
<info_vars_funcs_options
> {
5238 [] (info_vars_funcs_options
*opt
) { return &opt
->quiet
; },
5239 nullptr, /* show_cmd_cb */
5240 nullptr /* set_doc */
5243 gdb::option::boolean_option_def
<info_vars_funcs_options
> {
5245 [] (info_vars_funcs_options
*opt
) { return &opt
->exclude_minsyms
; },
5246 nullptr, /* show_cmd_cb */
5247 nullptr /* set_doc */
5250 gdb::option::string_option_def
<info_vars_funcs_options
> {
5252 [] (info_vars_funcs_options
*opt
) { return &opt
->type_regexp
; },
5253 nullptr, /* show_cmd_cb */
5254 nullptr /* set_doc */
5258 /* Returns the option group used by 'info variables' and 'info
5261 static gdb::option::option_def_group
5262 make_info_vars_funcs_options_def_group (info_vars_funcs_options
*opts
)
5264 return {{info_vars_funcs_options_defs
}, opts
};
5267 /* Command completer for 'info variables' and 'info functions'. */
5270 info_vars_funcs_command_completer (struct cmd_list_element
*ignore
,
5271 completion_tracker
&tracker
,
5272 const char *text
, const char * /* word */)
5275 = make_info_vars_funcs_options_def_group (nullptr);
5276 if (gdb::option::complete_options
5277 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
5280 const char *word
= advance_to_expression_complete_word_point (tracker
, text
);
5281 symbol_completer (ignore
, tracker
, text
, word
);
5284 /* Implement the 'info variables' command. */
5287 info_variables_command (const char *args
, int from_tty
)
5289 info_vars_funcs_options opts
;
5290 auto grp
= make_info_vars_funcs_options_def_group (&opts
);
5291 gdb::option::process_options
5292 (&args
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, grp
);
5293 if (args
!= nullptr && *args
== '\0')
5297 (opts
.quiet
, opts
.exclude_minsyms
, args
, VAR_DOMAIN
,
5298 opts
.type_regexp
.empty () ? nullptr : opts
.type_regexp
.c_str (),
5302 /* Implement the 'info functions' command. */
5305 info_functions_command (const char *args
, int from_tty
)
5307 info_vars_funcs_options opts
;
5309 auto grp
= make_info_vars_funcs_options_def_group (&opts
);
5310 gdb::option::process_options
5311 (&args
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, grp
);
5312 if (args
!= nullptr && *args
== '\0')
5316 (opts
.quiet
, opts
.exclude_minsyms
, args
, FUNCTION_DOMAIN
,
5317 opts
.type_regexp
.empty () ? nullptr : opts
.type_regexp
.c_str (),
5321 /* Holds the -q option for the 'info types' command. */
5323 struct info_types_options
5328 /* The options used by the 'info types' command. */
5330 static const gdb::option::option_def info_types_options_defs
[] = {
5331 gdb::option::boolean_option_def
<info_types_options
> {
5333 [] (info_types_options
*opt
) { return &opt
->quiet
; },
5334 nullptr, /* show_cmd_cb */
5335 nullptr /* set_doc */
5339 /* Returns the option group used by 'info types'. */
5341 static gdb::option::option_def_group
5342 make_info_types_options_def_group (info_types_options
*opts
)
5344 return {{info_types_options_defs
}, opts
};
5347 /* Implement the 'info types' command. */
5350 info_types_command (const char *args
, int from_tty
)
5352 info_types_options opts
;
5354 auto grp
= make_info_types_options_def_group (&opts
);
5355 gdb::option::process_options
5356 (&args
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, grp
);
5357 if (args
!= nullptr && *args
== '\0')
5359 symtab_symbol_info (opts
.quiet
, false, args
, TYPE_DOMAIN
, nullptr,
5363 /* Command completer for 'info types' command. */
5366 info_types_command_completer (struct cmd_list_element
*ignore
,
5367 completion_tracker
&tracker
,
5368 const char *text
, const char * /* word */)
5371 = make_info_types_options_def_group (nullptr);
5372 if (gdb::option::complete_options
5373 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
5376 const char *word
= advance_to_expression_complete_word_point (tracker
, text
);
5377 symbol_completer (ignore
, tracker
, text
, word
);
5380 /* Implement the 'info modules' command. */
5383 info_modules_command (const char *args
, int from_tty
)
5385 info_types_options opts
;
5387 auto grp
= make_info_types_options_def_group (&opts
);
5388 gdb::option::process_options
5389 (&args
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, grp
);
5390 if (args
!= nullptr && *args
== '\0')
5392 symtab_symbol_info (opts
.quiet
, true, args
, MODULE_DOMAIN
, nullptr,
5396 /* Implement the 'info main' command. */
5399 info_main_command (const char *args
, int from_tty
)
5401 gdb_printf ("%s\n", main_name ());
5405 rbreak_command (const char *regexp
, int from_tty
)
5407 gdb::unique_xmalloc_ptr
<char> file_name
;
5409 if (regexp
!= nullptr)
5411 const char *colon
= strchr (regexp
, ':');
5413 /* Ignore the colon if it is part of a Windows drive. */
5414 if (HAS_DRIVE_SPEC (regexp
)
5415 && (regexp
[2] == '/' || regexp
[2] == '\\'))
5416 colon
= strchr (STRIP_DRIVE_SPEC (regexp
), ':');
5418 if (colon
&& *(colon
+ 1) != ':')
5420 int colon_index
= colon
- regexp
;
5421 while (colon_index
> 0 && c_isspace (regexp
[colon_index
- 1]))
5424 file_name
= make_unique_xstrndup (regexp
, colon_index
);
5425 regexp
= skip_spaces (colon
+ 1);
5429 global_symbol_searcher
spec (SEARCH_FUNCTION_DOMAIN
, regexp
);
5430 if (file_name
!= nullptr)
5431 spec
.add_filename (std::move (file_name
));
5432 std::vector
<symbol_search
> symbols
= spec
.search ();
5434 gdb::unordered_set
<std::string
> seen_names
;
5435 scoped_rbreak_breakpoints finalize
;
5438 for (const symbol_search
&p
: symbols
)
5441 if (p
.msymbol
.minsym
== nullptr)
5443 if (file_name
!= nullptr)
5445 struct symtab
*symtab
= p
.symbol
->symtab ();
5446 const char *fullname
= symtab_to_fullname (symtab
);
5447 name
= string_printf ("%s:'%s'", fullname
,
5448 p
.symbol
->linkage_name ());
5451 name
= p
.symbol
->linkage_name ();
5454 name
= p
.msymbol
.minsym
->linkage_name ();
5456 if (!seen_names
.insert (name
).second
)
5461 break_command (name
.c_str (), from_tty
);
5463 catch (const gdb_exception_error
&ex
)
5465 exception_print (gdb_stderr
, ex
);
5470 if (p
.msymbol
.minsym
== nullptr)
5471 print_symbol_info (p
.symbol
, p
.block
, nullptr);
5473 gdb_printf ("<function, no debug info> %s;\n", name
.c_str ());
5476 int first_bp
= finalize
.first_breakpoint ();
5477 int last_bp
= finalize
.last_breakpoint ();
5480 gdb_printf (_("No breakpoints made.\n"));
5481 else if (first_bp
== last_bp
)
5482 gdb_printf (_("Successfully created breakpoint %d.\n"), first_bp
);
5484 gdb_printf (_("Successfully created breakpoints %d-%d.\n"),
5488 gdb_printf (_("%d breakpoints failed due to errors, see above.\n"),
5493 /* Evaluate if SYMNAME matches LOOKUP_NAME. */
5496 compare_symbol_name (const char *symbol_name
, language symbol_language
,
5497 const lookup_name_info
&lookup_name
,
5498 completion_match_result
&match_res
)
5500 const language_defn
*lang
= language_def (symbol_language
);
5502 symbol_name_matcher_ftype
*name_match
5503 = lang
->get_symbol_name_matcher (lookup_name
);
5505 return name_match (symbol_name
, lookup_name
, &match_res
);
5511 completion_list_add_name (completion_tracker
&tracker
,
5512 language symbol_language
,
5513 const char *symname
,
5514 const lookup_name_info
&lookup_name
,
5515 const char *text
, const char *word
)
5517 completion_match_result
&match_res
5518 = tracker
.reset_completion_match_result ();
5520 /* Clip symbols that cannot match. */
5521 if (!compare_symbol_name (symname
, symbol_language
, lookup_name
, match_res
))
5524 /* Refresh SYMNAME from the match string. It's potentially
5525 different depending on language. (E.g., on Ada, the match may be
5526 the encoded symbol name wrapped in "<>"). */
5527 symname
= match_res
.match
.match ();
5528 gdb_assert (symname
!= NULL
);
5530 /* We have a match for a completion, so add SYMNAME to the current list
5531 of matches. Note that the name is moved to freshly malloc'd space. */
5534 gdb::unique_xmalloc_ptr
<char> completion
5535 = make_completion_match_str (symname
, text
, word
);
5537 /* Here we pass the match-for-lcd object to add_completion. Some
5538 languages match the user text against substrings of symbol
5539 names in some cases. E.g., in C++, "b push_ba" completes to
5540 "std::vector::push_back", "std::string::push_back", etc., and
5541 in this case we want the completion lowest common denominator
5542 to be "push_back" instead of "std::". */
5543 tracker
.add_completion (std::move (completion
),
5544 &match_res
.match_for_lcd
, text
, word
);
5550 /* completion_list_add_name wrapper for struct symbol. */
5553 completion_list_add_symbol (completion_tracker
&tracker
,
5555 const lookup_name_info
&lookup_name
,
5556 const char *text
, const char *word
)
5558 if (!completion_list_add_name (tracker
, sym
->language (),
5559 sym
->natural_name (),
5560 lookup_name
, text
, word
))
5563 /* C++ function symbols include the parameters within both the msymbol
5564 name and the symbol name. The problem is that the msymbol name will
5565 describe the parameters in the most basic way, with typedefs stripped
5566 out, while the symbol name will represent the types as they appear in
5567 the program. This means we will see duplicate entries in the
5568 completion tracker. The following converts the symbol name back to
5569 the msymbol name and removes the msymbol name from the completion
5571 if (sym
->language () == language_cplus
5572 && sym
->loc_class () == LOC_BLOCK
)
5574 /* The call to canonicalize returns the empty string if the input
5575 string is already in canonical form, thanks to this we don't
5576 remove the symbol we just added above. */
5577 gdb::unique_xmalloc_ptr
<char> str
5578 = cp_canonicalize_string_no_typedefs (sym
->natural_name ());
5580 tracker
.remove_completion (str
.get ());
5584 /* completion_list_add_name wrapper for struct minimal_symbol. */
5587 completion_list_add_msymbol (completion_tracker
&tracker
,
5588 minimal_symbol
*sym
,
5589 const lookup_name_info
&lookup_name
,
5590 const char *text
, const char *word
)
5592 completion_list_add_name (tracker
, sym
->language (),
5593 sym
->natural_name (),
5594 lookup_name
, text
, word
);
5598 /* ObjC: In case we are completing on a selector, look as the msymbol
5599 again and feed all the selectors into the mill. */
5602 completion_list_objc_symbol (completion_tracker
&tracker
,
5603 struct minimal_symbol
*msymbol
,
5604 const lookup_name_info
&lookup_name
,
5605 const char *text
, const char *word
)
5607 static char *tmp
= NULL
;
5608 static unsigned int tmplen
= 0;
5610 const char *method
, *category
, *selector
;
5613 method
= msymbol
->natural_name ();
5615 /* Is it a method? */
5616 if ((method
[0] != '-') && (method
[0] != '+'))
5620 /* Complete on shortened method method. */
5621 completion_list_add_name (tracker
, language_objc
,
5626 while ((strlen (method
) + 1) >= tmplen
)
5632 tmp
= (char *) xrealloc (tmp
, tmplen
);
5634 selector
= strchr (method
, ' ');
5635 if (selector
!= NULL
)
5638 category
= strchr (method
, '(');
5640 if ((category
!= NULL
) && (selector
!= NULL
))
5642 memcpy (tmp
, method
, (category
- method
));
5643 tmp
[category
- method
] = ' ';
5644 memcpy (tmp
+ (category
- method
) + 1, selector
, strlen (selector
) + 1);
5645 completion_list_add_name (tracker
, language_objc
, tmp
,
5646 lookup_name
, text
, word
);
5648 completion_list_add_name (tracker
, language_objc
, tmp
+ 1,
5649 lookup_name
, text
, word
);
5652 if (selector
!= NULL
)
5654 /* Complete on selector only. */
5655 strcpy (tmp
, selector
);
5656 tmp2
= strchr (tmp
, ']');
5660 completion_list_add_name (tracker
, language_objc
, tmp
,
5661 lookup_name
, text
, word
);
5665 /* Break the non-quoted text based on the characters which are in
5666 symbols. FIXME: This should probably be language-specific. */
5669 language_search_unquoted_string (const char *text
, const char *p
)
5671 for (; p
> text
; --p
)
5673 if (c_isalnum (p
[-1]) || p
[-1] == '_' || p
[-1] == '\0')
5677 if ((current_language
->la_language
== language_objc
))
5679 if (p
[-1] == ':') /* Might be part of a method name. */
5681 else if (p
[-1] == '[' && (p
[-2] == '-' || p
[-2] == '+'))
5682 p
-= 2; /* Beginning of a method name. */
5683 else if (p
[-1] == ' ' || p
[-1] == '(' || p
[-1] == ')')
5684 { /* Might be part of a method name. */
5687 /* Seeing a ' ' or a '(' is not conclusive evidence
5688 that we are in the middle of a method name. However,
5689 finding "-[" or "+[" should be pretty un-ambiguous.
5690 Unfortunately we have to find it now to decide. */
5693 if (c_isalnum (t
[-1]) || t
[-1] == '_' ||
5694 t
[-1] == ' ' || t
[-1] == ':' ||
5695 t
[-1] == '(' || t
[-1] == ')')
5700 if (t
[-1] == '[' && (t
[-2] == '-' || t
[-2] == '+'))
5701 p
= t
- 2; /* Method name detected. */
5702 /* Else we leave with p unchanged. */
5712 completion_list_add_fields (completion_tracker
&tracker
,
5714 const lookup_name_info
&lookup_name
,
5715 const char *text
, const char *word
)
5717 if (sym
->loc_class () == LOC_TYPEDEF
)
5719 struct type
*t
= sym
->type ();
5720 enum type_code c
= t
->code ();
5723 if (c
== TYPE_CODE_UNION
|| c
== TYPE_CODE_STRUCT
)
5724 for (j
= TYPE_N_BASECLASSES (t
); j
< t
->num_fields (); j
++)
5725 if (t
->field (j
).name ())
5726 completion_list_add_name (tracker
, sym
->language (),
5727 t
->field (j
).name (),
5728 lookup_name
, text
, word
);
5735 symbol_is_function_or_method (symbol
*sym
)
5737 switch (sym
->type ()->code ())
5739 case TYPE_CODE_FUNC
:
5740 case TYPE_CODE_METHOD
:
5750 symbol_is_function_or_method (minimal_symbol
*msymbol
)
5752 switch (msymbol
->type ())
5755 case mst_text_gnu_ifunc
:
5756 case mst_solib_trampoline
:
5766 bound_minimal_symbol
5767 find_gnu_ifunc (const symbol
*sym
)
5769 if (sym
->loc_class () != LOC_BLOCK
)
5772 lookup_name_info
lookup_name (sym
->search_name (),
5773 symbol_name_match_type::SEARCH_NAME
);
5774 struct objfile
*objfile
= sym
->objfile ();
5776 CORE_ADDR address
= sym
->value_block ()->entry_pc ();
5777 minimal_symbol
*ifunc
= NULL
;
5779 iterate_over_minimal_symbols (objfile
, lookup_name
,
5780 [&] (minimal_symbol
*minsym
)
5782 if (minsym
->type () == mst_text_gnu_ifunc
5783 || minsym
->type () == mst_data_gnu_ifunc
)
5785 CORE_ADDR msym_addr
= minsym
->value_address (objfile
);
5786 if (minsym
->type () == mst_data_gnu_ifunc
)
5788 struct gdbarch
*gdbarch
= objfile
->arch ();
5789 msym_addr
= gdbarch_convert_from_func_ptr_addr
5790 (gdbarch
, msym_addr
, current_inferior ()->top_target ());
5792 if (msym_addr
== address
)
5802 return {ifunc
, objfile
};
5806 /* Add matching symbols from SYMTAB to the current completion list. */
5809 add_symtab_completions (struct compunit_symtab
*cust
,
5810 completion_tracker
&tracker
,
5811 complete_symbol_mode mode
,
5812 const lookup_name_info
&lookup_name
,
5813 const char *text
, const char *word
,
5814 enum type_code code
)
5821 for (i
= GLOBAL_BLOCK
; i
<= STATIC_BLOCK
; i
++)
5825 const struct block
*b
= cust
->blockvector ()->block (i
);
5826 for (struct symbol
*sym
: block_iterator_range (b
))
5828 if (completion_skip_symbol (mode
, sym
))
5831 if (code
== TYPE_CODE_UNDEF
5832 || (sym
->domain () == STRUCT_DOMAIN
5833 && sym
->type ()->code () == code
))
5834 completion_list_add_symbol (tracker
, sym
,
5842 default_collect_symbol_completion_matches_break_on
5843 (completion_tracker
&tracker
, complete_symbol_mode mode
,
5844 symbol_name_match_type name_match_type
,
5845 const char *text
, const char *word
,
5846 const char *break_on
, enum type_code code
)
5848 /* Problem: All of the symbols have to be copied because readline
5849 frees them. I'm not going to worry about this; hopefully there
5850 won't be that many. */
5852 const struct block
*b
;
5853 const struct block
*surrounding_static_block
, *surrounding_global_block
;
5854 /* The symbol we are completing on. Points in same buffer as text. */
5855 const char *sym_text
;
5857 /* Now look for the symbol we are supposed to complete on. */
5858 if (mode
== complete_symbol_mode::LINESPEC
)
5864 const char *quote_pos
= NULL
;
5866 /* First see if this is a quoted string. */
5868 for (p
= text
; *p
!= '\0'; ++p
)
5870 if (quote_found
!= '\0')
5872 if (*p
== quote_found
)
5873 /* Found close quote. */
5875 else if (*p
== '\\' && p
[1] == quote_found
)
5876 /* A backslash followed by the quote character
5877 doesn't end the string. */
5880 else if (*p
== '\'' || *p
== '"')
5886 if (quote_found
== '\'')
5887 /* A string within single quotes can be a symbol, so complete on it. */
5888 sym_text
= quote_pos
+ 1;
5889 else if (quote_found
== '"')
5890 /* A double-quoted string is never a symbol, nor does it make sense
5891 to complete it any other way. */
5897 /* It is not a quoted string. Break it based on the characters
5898 which are in symbols. */
5901 if (c_isalnum (p
[-1]) || p
[-1] == '_' || p
[-1] == '\0'
5902 || p
[-1] == ':' || strchr (break_on
, p
[-1]) != NULL
)
5911 lookup_name_info
lookup_name (sym_text
, name_match_type
, true);
5913 /* At this point scan through the misc symbol vectors and add each
5914 symbol you find to the list. Eventually we want to ignore
5915 anything that isn't a text symbol (everything else will be
5916 handled by the psymtab code below). */
5918 if (code
== TYPE_CODE_UNDEF
)
5920 for (objfile
&objfile
: current_program_space
->objfiles ())
5922 for (minimal_symbol
*msymbol
: objfile
.msymbols ())
5926 if (completion_skip_symbol (mode
, msymbol
))
5929 completion_list_add_msymbol (tracker
, msymbol
, lookup_name
,
5932 completion_list_objc_symbol (tracker
, msymbol
, lookup_name
,
5938 /* Add completions for all currently loaded symbol tables. */
5939 for (objfile
&objfile
: current_program_space
->objfiles ())
5941 /* Look through the partial symtabs for all symbols which begin by
5942 matching SYM_TEXT. Expand all CUs that you find to the list. */
5944 (nullptr, &lookup_name
, nullptr,
5945 [&] (compunit_symtab
*symtab
)
5947 add_symtab_completions (symtab
,
5948 tracker
, mode
, lookup_name
,
5949 sym_text
, word
, code
);
5952 SEARCH_GLOBAL_BLOCK
| SEARCH_STATIC_BLOCK
,
5953 SEARCH_ALL_DOMAINS
);
5956 /* Search upwards from currently selected frame (so that we can
5957 complete on local vars). Also catch fields of types defined in
5958 this places which match our text string. Only complete on types
5959 visible from current context. */
5961 b
= get_selected_block (0);
5962 surrounding_static_block
= b
== nullptr ? nullptr : b
->static_block ();
5963 surrounding_global_block
= b
== nullptr ? nullptr : b
->global_block ();
5964 if (surrounding_static_block
!= NULL
)
5965 while (b
!= surrounding_static_block
)
5969 for (struct symbol
*sym
: block_iterator_range (b
))
5971 if (code
== TYPE_CODE_UNDEF
)
5973 completion_list_add_symbol (tracker
, sym
, lookup_name
,
5975 completion_list_add_fields (tracker
, sym
, lookup_name
,
5978 else if (sym
->domain () == STRUCT_DOMAIN
5979 && sym
->type ()->code () == code
)
5980 completion_list_add_symbol (tracker
, sym
, lookup_name
,
5984 /* Stop when we encounter an enclosing function. Do not stop for
5985 non-inlined functions - the locals of the enclosing function
5986 are in scope for a nested function. */
5987 if (b
->function () != NULL
&& b
->inlined_p ())
5989 b
= b
->superblock ();
5992 /* Add fields from the file's types; symbols will be added below. */
5994 if (code
== TYPE_CODE_UNDEF
)
5996 if (surrounding_static_block
!= NULL
)
5997 for (struct symbol
*sym
: block_iterator_range (surrounding_static_block
))
5998 completion_list_add_fields (tracker
, sym
, lookup_name
,
6001 if (surrounding_global_block
!= NULL
)
6002 for (struct symbol
*sym
: block_iterator_range (surrounding_global_block
))
6003 completion_list_add_fields (tracker
, sym
, lookup_name
,
6007 /* Skip macros if we are completing a struct tag -- arguable but
6008 usually what is expected. */
6009 if (current_language
->macro_expansion () == macro_expansion_c
6010 && code
== TYPE_CODE_UNDEF
)
6012 /* This adds a macro's name to the current completion list. */
6013 auto add_macro_name
= [&] (const char *macro_name
,
6014 const macro_definition
*,
6015 macro_source_file
*,
6018 completion_list_add_name (tracker
, language_c
, macro_name
,
6019 lookup_name
, sym_text
, word
);
6022 /* Add any macros visible in the default scope. Note that this
6023 may yield the occasional wrong result, because an expression
6024 might be evaluated in a scope other than the default. For
6025 example, if the user types "break file:line if <TAB>", the
6026 resulting expression will be evaluated at "file:line" -- but
6027 at there does not seem to be a way to detect this at
6029 macro_scope scope
= default_macro_scope ();
6030 if (scope
.is_valid ())
6031 macro_for_each_in_scope (scope
.file
, scope
.line
, add_macro_name
);
6033 /* User-defined macros are always visible. */
6034 macro_for_each (macro_user_macros
, add_macro_name
);
6038 /* Collect all symbols (regardless of class) which begin by matching
6042 collect_symbol_completion_matches (completion_tracker
&tracker
,
6043 complete_symbol_mode mode
,
6044 symbol_name_match_type name_match_type
,
6045 const char *text
, const char *word
)
6047 current_language
->collect_symbol_completion_matches (tracker
, mode
,
6053 /* Like collect_symbol_completion_matches, but only collect
6054 STRUCT_DOMAIN symbols whose type code is CODE. */
6057 collect_symbol_completion_matches_type (completion_tracker
&tracker
,
6058 const char *text
, const char *word
,
6059 enum type_code code
)
6061 complete_symbol_mode mode
= complete_symbol_mode::EXPRESSION
;
6062 symbol_name_match_type name_match_type
= symbol_name_match_type::EXPRESSION
;
6064 gdb_assert (code
== TYPE_CODE_UNION
6065 || code
== TYPE_CODE_STRUCT
6066 || code
== TYPE_CODE_ENUM
);
6067 current_language
->collect_symbol_completion_matches (tracker
, mode
,
6072 /* Like collect_symbol_completion_matches, but collects a list of
6073 symbols defined in all source files named SRCFILE. */
6076 collect_file_symbol_completion_matches (completion_tracker
&tracker
,
6077 complete_symbol_mode mode
,
6078 symbol_name_match_type name_match_type
,
6079 const char *text
, const char *word
,
6080 const char *srcfile
)
6082 /* The symbol we are completing on. Points in same buffer as text. */
6083 const char *sym_text
;
6085 /* Now look for the symbol we are supposed to complete on.
6086 FIXME: This should be language-specific. */
6087 if (mode
== complete_symbol_mode::LINESPEC
)
6093 const char *quote_pos
= NULL
;
6095 /* First see if this is a quoted string. */
6097 for (p
= text
; *p
!= '\0'; ++p
)
6099 if (quote_found
!= '\0')
6101 if (*p
== quote_found
)
6102 /* Found close quote. */
6104 else if (*p
== '\\' && p
[1] == quote_found
)
6105 /* A backslash followed by the quote character
6106 doesn't end the string. */
6109 else if (*p
== '\'' || *p
== '"')
6115 if (quote_found
== '\'')
6116 /* A string within single quotes can be a symbol, so complete on it. */
6117 sym_text
= quote_pos
+ 1;
6118 else if (quote_found
== '"')
6119 /* A double-quoted string is never a symbol, nor does it make sense
6120 to complete it any other way. */
6126 /* Not a quoted string. */
6127 sym_text
= language_search_unquoted_string (text
, p
);
6131 lookup_name_info
lookup_name (sym_text
, name_match_type
, true);
6133 /* Go through symtabs for SRCFILE and check the externs and statics
6134 for symbols which match. */
6135 iterate_over_symtabs (current_program_space
, srcfile
, [&] (symtab
*s
)
6137 add_symtab_completions (s
->compunit (),
6138 tracker
, mode
, lookup_name
,
6139 sym_text
, word
, TYPE_CODE_UNDEF
);
6144 /* A helper function for make_source_files_completion_list. It adds
6145 another file name to a list of possible completions, growing the
6146 list as necessary. */
6149 add_filename_to_list (const char *fname
, const char *text
, const char *word
,
6150 completion_list
*list
)
6152 list
->emplace_back (make_completion_match_str (fname
, text
, word
));
6156 not_interesting_fname (const char *fname
)
6158 static const char *illegal_aliens
[] = {
6159 "_globals_", /* inserted by coff_symtab_read */
6164 for (i
= 0; illegal_aliens
[i
]; i
++)
6166 if (filename_cmp (fname
, illegal_aliens
[i
]) == 0)
6172 /* An object of this type is passed as the callback argument to
6173 map_partial_symbol_filenames. */
6174 struct add_partial_filename_data
6176 struct filename_seen_cache
*filename_seen_cache
;
6180 completion_list
*list
;
6182 void operator() (const char *filename
, const char *fullname
);
6185 /* A callback for map_partial_symbol_filenames. */
6188 add_partial_filename_data::operator() (const char *filename
,
6189 const char *fullname
)
6191 if (not_interesting_fname (filename
))
6193 if (!filename_seen_cache
->seen (filename
)
6194 && filename_ncmp (filename
, text
, text_len
) == 0)
6196 /* This file matches for a completion; add it to the
6197 current list of matches. */
6198 add_filename_to_list (filename
, text
, word
, list
);
6202 const char *base_name
= lbasename (filename
);
6204 if (base_name
!= filename
6205 && !filename_seen_cache
->seen (base_name
)
6206 && filename_ncmp (base_name
, text
, text_len
) == 0)
6207 add_filename_to_list (base_name
, text
, word
, list
);
6211 /* Return a list of all source files whose names begin with matching
6212 TEXT. The file names are looked up in the symbol tables of this
6216 make_source_files_completion_list (const char *text
)
6218 size_t text_len
= strlen (text
);
6219 completion_list list
;
6220 const char *base_name
;
6221 struct add_partial_filename_data datum
;
6223 if (!have_full_symbols (current_program_space
)
6224 && !have_partial_symbols (current_program_space
))
6227 filename_seen_cache filenames_seen
;
6229 for (objfile
&objfile
: current_program_space
->objfiles ())
6231 for (compunit_symtab
&cu
: objfile
.compunits ())
6233 for (symtab
*s
: cu
.filetabs ())
6235 if (not_interesting_fname (s
->filename ()))
6237 if (!filenames_seen
.seen (s
->filename ())
6238 && filename_ncmp (s
->filename (), text
, text_len
) == 0)
6240 /* This file matches for a completion; add it to the current
6242 add_filename_to_list (s
->filename (), text
, text
, &list
);
6246 /* NOTE: We allow the user to type a base name when the
6247 debug info records leading directories, but not the other
6248 way around. This is what subroutines of breakpoint
6249 command do when they parse file names. */
6250 base_name
= lbasename (s
->filename ());
6251 if (base_name
!= s
->filename ()
6252 && !filenames_seen
.seen (base_name
)
6253 && filename_ncmp (base_name
, text
, text_len
) == 0)
6254 add_filename_to_list (base_name
, text
, text
, &list
);
6260 datum
.filename_seen_cache
= &filenames_seen
;
6263 datum
.text_len
= text_len
;
6265 current_program_space
->map_symbol_filenames (datum
, false /*need_fullname*/);
6272 /* Return the "main_info" object for the current program space. If
6273 the object has not yet been created, create it and fill in some
6277 get_main_info (program_space
*pspace
)
6279 main_info
*info
= main_progspace_key
.get (pspace
);
6283 /* It may seem strange to store the main name in the progspace
6284 and also in whatever objfile happens to see a main name in
6285 its debug info. The reason for this is mainly historical:
6286 gdb returned "main" as the name even if no function named
6287 "main" was defined the program; and this approach lets us
6288 keep compatibility. */
6289 info
= main_progspace_key
.emplace (pspace
);
6296 set_main_name (program_space
*pspace
, const char *name
, enum language lang
)
6298 main_info
*info
= get_main_info (pspace
);
6300 if (!info
->name_of_main
.empty ())
6302 info
->name_of_main
.clear ();
6303 info
->language_of_main
= language_unknown
;
6307 info
->name_of_main
= name
;
6308 info
->language_of_main
= lang
;
6312 /* Deduce the name of the main procedure, and set NAME_OF_MAIN
6316 find_main_name (void)
6318 const char *new_main_name
;
6319 program_space
*pspace
= current_program_space
;
6321 /* First check the objfiles to see whether a debuginfo reader has
6322 picked up the appropriate main name. Historically the main name
6323 was found in a more or less random way; this approach instead
6324 relies on the order of objfile creation -- which still isn't
6325 guaranteed to get the correct answer, but is just probably more
6327 for (objfile
&objfile
: current_program_space
->objfiles ())
6329 objfile
.compute_main_name ();
6331 if (objfile
.per_bfd
->name_of_main
!= NULL
)
6333 set_main_name (pspace
,
6334 objfile
.per_bfd
->name_of_main
,
6335 objfile
.per_bfd
->language_of_main
);
6340 /* Try to see if the main procedure is in Ada. */
6341 /* FIXME: brobecker/2005-03-07: Another way of doing this would
6342 be to add a new method in the language vector, and call this
6343 method for each language until one of them returns a non-empty
6344 name. This would allow us to remove this hard-coded call to
6345 an Ada function. It is not clear that this is a better approach
6346 at this point, because all methods need to be written in a way
6347 such that false positives never be returned. For instance, it is
6348 important that a method does not return a wrong name for the main
6349 procedure if the main procedure is actually written in a different
6350 language. It is easy to guaranty this with Ada, since we use a
6351 special symbol generated only when the main in Ada to find the name
6352 of the main procedure. It is difficult however to see how this can
6353 be guaranteed for languages such as C, for instance. This suggests
6354 that order of call for these methods becomes important, which means
6355 a more complicated approach. */
6356 new_main_name
= ada_main_name ();
6357 if (new_main_name
!= NULL
)
6359 set_main_name (pspace
, new_main_name
, language_ada
);
6363 new_main_name
= d_main_name ();
6364 if (new_main_name
!= NULL
)
6366 set_main_name (pspace
, new_main_name
, language_d
);
6370 new_main_name
= go_main_name ();
6371 if (new_main_name
!= NULL
)
6373 set_main_name (pspace
, new_main_name
, language_go
);
6377 new_main_name
= pascal_main_name ();
6378 if (new_main_name
!= NULL
)
6380 set_main_name (pspace
, new_main_name
, language_pascal
);
6384 /* The languages above didn't identify the name of the main procedure.
6385 Fallback to "main". */
6387 /* Try to find language for main in psymtabs. */
6388 bool symbol_found_p
= false;
6389 current_program_space
->iterate_over_objfiles_in_search_order
6390 ([&symbol_found_p
, pspace
] (objfile
*obj
)
6393 = obj
->lookup_global_symbol_language ("main",
6394 SEARCH_FUNCTION_DOMAIN
,
6398 set_main_name (pspace
, "main", lang
);
6408 set_main_name (pspace
, "main", language_unknown
);
6416 main_info
*info
= get_main_info (current_program_space
);
6418 if (info
->name_of_main
.empty ())
6421 return info
->name_of_main
.c_str ();
6424 /* Return the language of the main function. If it is not known,
6425 return language_unknown. */
6428 main_language (void)
6430 main_info
*info
= get_main_info (current_program_space
);
6432 if (info
->name_of_main
.empty ())
6435 return info
->language_of_main
;
6440 /* The next index to hand out in response to a registration request. */
6442 static int next_loc_class_value
= LOC_FINAL_VALUE
;
6444 /* The maximum number of "loc_class" registrations we support. This is
6445 constant for convenience. */
6446 #define MAX_SYMBOL_IMPLS (LOC_FINAL_VALUE + 11)
6448 /* The objects representing the various "loc_class" values. The elements
6449 from 0 up to LOC_FINAL_VALUE-1 represent themselves, and subsequent
6450 elements are those registered at gdb initialization time. */
6452 static struct symbol_impl symbol_impl
[MAX_SYMBOL_IMPLS
];
6454 /* The globally visible pointer. This is separate from 'symbol_impl'
6455 so that it can be const. */
6457 gdb::array_view
<const struct symbol_impl
> symbol_impls (symbol_impl
);
6459 /* Make sure we saved enough room in struct symbol. */
6461 static_assert (MAX_SYMBOL_IMPLS
<= (1 << SYMBOL_LOC_CLASS_BITS
));
6463 /* Register a computed symbol type. LOC_CLASS must be LOC_COMPUTED. OPS
6464 is the ops vector associated with this index. This returns the new
6465 index, which should be used as the loc_class_index field for symbols
6469 register_symbol_computed_impl (location_class loc_class
,
6470 const struct symbol_computed_ops
*ops
)
6472 int result
= next_loc_class_value
++;
6474 gdb_assert (loc_class
== LOC_COMPUTED
);
6475 gdb_assert (result
< MAX_SYMBOL_IMPLS
);
6476 symbol_impl
[result
].loc_class
= loc_class
;
6477 symbol_impl
[result
].ops_computed
= ops
;
6479 /* Sanity check OPS. */
6480 gdb_assert (ops
!= NULL
);
6481 gdb_assert (ops
->tracepoint_var_ref
!= NULL
);
6482 gdb_assert (ops
->describe_location
!= NULL
);
6483 gdb_assert (ops
->get_symbol_read_needs
!= NULL
);
6484 gdb_assert (ops
->read_variable
!= NULL
);
6489 /* Register a function with frame base type. LOC_CLASS must be LOC_BLOCK.
6490 OPS is the ops vector associated with this index. This returns the
6491 new index, which should be used as the loc_class_index field for symbols
6495 register_symbol_block_impl (location_class loc_class
,
6496 const struct symbol_block_ops
*ops
)
6498 int result
= next_loc_class_value
++;
6500 gdb_assert (loc_class
== LOC_BLOCK
);
6501 gdb_assert (result
< MAX_SYMBOL_IMPLS
);
6502 symbol_impl
[result
].loc_class
= loc_class
;
6503 symbol_impl
[result
].ops_block
= ops
;
6505 /* Sanity check OPS. */
6506 gdb_assert (ops
!= NULL
);
6507 gdb_assert (ops
->find_frame_base_location
!= nullptr
6508 || ops
->get_block_value
!= nullptr);
6513 /* Register a register symbol type. LOC_CLASS must be LOC_REGISTER or
6514 LOC_REGPARM_ADDR. OPS is the register ops vector associated with
6515 this index. This returns the new index, which should be used as
6516 the loc_class_index field for symbols of this type. */
6519 register_symbol_register_impl (location_class loc_class
,
6520 const struct symbol_register_ops
*ops
)
6522 int result
= next_loc_class_value
++;
6524 gdb_assert (loc_class
== LOC_REGISTER
|| loc_class
== LOC_REGPARM_ADDR
);
6525 gdb_assert (result
< MAX_SYMBOL_IMPLS
);
6526 symbol_impl
[result
].loc_class
= loc_class
;
6527 symbol_impl
[result
].ops_register
= ops
;
6532 /* Initialize elements of 'symbol_impl' for the constants in enum
6536 initialize_ordinary_address_classes (void)
6538 for (int i
= 0; i
< LOC_FINAL_VALUE
; ++i
)
6539 symbol_impl
[i
].loc_class
= static_cast<location_class
> (i
);
6547 symbol::objfile () const
6549 gdb_assert (is_objfile_owned ());
6550 return owner
.symtab
->compunit ()->objfile ();
6556 symbol::arch () const
6558 if (!is_objfile_owned ())
6560 return owner
.symtab
->compunit ()->objfile ()->arch ();
6566 symbol::symtab () const
6568 gdb_assert (is_objfile_owned ());
6569 return owner
.symtab
;
6575 symbol::set_symtab (struct symtab
*symtab
)
6577 gdb_assert (is_objfile_owned ());
6578 owner
.symtab
= symtab
;
6584 symbol::get_maybe_copied_address () const
6586 gdb_assert (this->maybe_copied
);
6587 gdb_assert (this->loc_class () == LOC_STATIC
);
6589 const char *linkage_name
= this->linkage_name ();
6590 bound_minimal_symbol minsym
6591 = lookup_minimal_symbol_linkage (this->objfile ()->pspace (), linkage_name
,
6593 if (minsym
.minsym
!= nullptr)
6594 return minsym
.value_address ();
6596 return this->m_value
.address
;
6602 symbol::value_block () const
6604 if (const symbol_block_ops
*block_ops
= this->block_ops ();
6605 block_ops
!= nullptr && block_ops
->get_block_value
!= nullptr)
6606 return block_ops
->get_block_value (this);
6608 return m_value
.block
;
6614 symbol::relocate (gdb::array_view
<const CORE_ADDR
> delta
)
6616 if ((loc_class () == LOC_LABEL
|| loc_class () == LOC_STATIC
)
6617 && section_index () >= 0)
6618 set_value_address (value_address () + delta
[section_index ()]);
6624 minimal_symbol::get_maybe_copied_address (objfile
*objf
) const
6626 gdb_assert (this->maybe_copied (objf
));
6627 gdb_assert ((objf
->flags
& OBJF_MAINLINE
) == 0);
6629 const char *linkage_name
= this->linkage_name ();
6630 bound_minimal_symbol found
6631 = lookup_minimal_symbol_linkage (objf
->pspace (), linkage_name
,
6633 if (found
.minsym
!= nullptr)
6634 return found
.value_address ();
6636 return (this->m_value
.address
6637 + objf
->section_offsets
[this->section_index ()]);
6642 /* Hold the sub-commands of 'info module'. */
6644 static struct cmd_list_element
*info_module_cmdlist
= NULL
;
6648 std::vector
<module_symbol_search
>
6649 search_module_symbols (const char *module_regexp
, const char *regexp
,
6650 const char *type_regexp
, domain_search_flags kind
)
6652 std::vector
<module_symbol_search
> results
;
6654 /* Search for all modules matching MODULE_REGEXP. */
6655 global_symbol_searcher
spec1 (SEARCH_MODULE_DOMAIN
, module_regexp
);
6656 spec1
.set_exclude_minsyms (true);
6657 std::vector
<symbol_search
> modules
= spec1
.search ();
6659 /* Now search for all symbols of the required KIND matching the required
6660 regular expressions. We figure out which ones are in which modules
6662 global_symbol_searcher
spec2 (kind
, regexp
);
6663 spec2
.set_symbol_type_regexp (type_regexp
);
6664 spec2
.set_exclude_minsyms (true);
6665 std::vector
<symbol_search
> symbols
= spec2
.search ();
6667 /* Now iterate over all MODULES, checking to see which items from
6668 SYMBOLS are in each module. */
6669 for (const symbol_search
&p
: modules
)
6673 /* This is a module. */
6674 gdb_assert (p
.symbol
!= nullptr);
6676 std::string prefix
= p
.symbol
->print_name ();
6679 for (const symbol_search
&q
: symbols
)
6681 if (q
.symbol
== nullptr)
6684 if (strncmp (q
.symbol
->print_name (), prefix
.c_str (),
6685 prefix
.size ()) != 0)
6688 results
.push_back ({p
, q
});
6695 /* Implement the core of both 'info module functions' and 'info module
6699 info_module_subcommand (bool quiet
, const char *module_regexp
,
6700 const char *regexp
, const char *type_regexp
,
6701 domain_search_flags kind
)
6703 gdb_assert (kind
== SEARCH_FUNCTION_DOMAIN
|| kind
== SEARCH_VAR_DOMAIN
);
6705 /* Print a header line. Don't build the header line bit by bit as this
6706 prevents internationalisation. */
6709 if (module_regexp
== nullptr)
6711 if (type_regexp
== nullptr)
6713 if (regexp
== nullptr)
6714 gdb_printf ((kind
== SEARCH_VAR_DOMAIN
6715 ? _("All variables in all modules:")
6716 : _("All functions in all modules:")));
6719 ((kind
== SEARCH_VAR_DOMAIN
6720 ? _("All variables matching regular expression"
6721 " \"%s\" in all modules:")
6722 : _("All functions matching regular expression"
6723 " \"%s\" in all modules:")),
6728 if (regexp
== nullptr)
6730 ((kind
== SEARCH_VAR_DOMAIN
6731 ? _("All variables with type matching regular "
6732 "expression \"%s\" in all modules:")
6733 : _("All functions with type matching regular "
6734 "expression \"%s\" in all modules:")),
6738 ((kind
== SEARCH_VAR_DOMAIN
6739 ? _("All variables matching regular expression "
6740 "\"%s\",\n\twith type matching regular "
6741 "expression \"%s\" in all modules:")
6742 : _("All functions matching regular expression "
6743 "\"%s\",\n\twith type matching regular "
6744 "expression \"%s\" in all modules:")),
6745 regexp
, type_regexp
);
6750 if (type_regexp
== nullptr)
6752 if (regexp
== nullptr)
6754 ((kind
== SEARCH_VAR_DOMAIN
6755 ? _("All variables in all modules matching regular "
6756 "expression \"%s\":")
6757 : _("All functions in all modules matching regular "
6758 "expression \"%s\":")),
6762 ((kind
== SEARCH_VAR_DOMAIN
6763 ? _("All variables matching regular expression "
6764 "\"%s\",\n\tin all modules matching regular "
6765 "expression \"%s\":")
6766 : _("All functions matching regular expression "
6767 "\"%s\",\n\tin all modules matching regular "
6768 "expression \"%s\":")),
6769 regexp
, module_regexp
);
6773 if (regexp
== nullptr)
6775 ((kind
== SEARCH_VAR_DOMAIN
6776 ? _("All variables with type matching regular "
6777 "expression \"%s\"\n\tin all modules matching "
6778 "regular expression \"%s\":")
6779 : _("All functions with type matching regular "
6780 "expression \"%s\"\n\tin all modules matching "
6781 "regular expression \"%s\":")),
6782 type_regexp
, module_regexp
);
6785 ((kind
== SEARCH_VAR_DOMAIN
6786 ? _("All variables matching regular expression "
6787 "\"%s\",\n\twith type matching regular expression "
6788 "\"%s\",\n\tin all modules matching regular "
6789 "expression \"%s\":")
6790 : _("All functions matching regular expression "
6791 "\"%s\",\n\twith type matching regular expression "
6792 "\"%s\",\n\tin all modules matching regular "
6793 "expression \"%s\":")),
6794 regexp
, type_regexp
, module_regexp
);
6800 /* Find all symbols of type KIND matching the given regular expressions
6801 along with the symbols for the modules in which those symbols
6803 std::vector
<module_symbol_search
> module_symbols
6804 = search_module_symbols (module_regexp
, regexp
, type_regexp
, kind
);
6806 std::sort (module_symbols
.begin (), module_symbols
.end (),
6807 [] (const module_symbol_search
&a
, const module_symbol_search
&b
)
6809 if (a
.first
< b
.first
)
6811 else if (a
.first
== b
.first
)
6812 return a
.second
< b
.second
;
6817 const char *last_filename
= "";
6818 const symbol
*last_module_symbol
= nullptr;
6819 for (const auto &[p
, q
] : module_symbols
)
6821 gdb_assert (q
.symbol
!= nullptr);
6823 if (last_module_symbol
!= p
.symbol
)
6826 gdb_printf (_("Module \"%s\":\n"), p
.symbol
->print_name ());
6827 last_module_symbol
= p
.symbol
;
6831 print_symbol_info (q
.symbol
, q
.block
, last_filename
);
6833 = symtab_to_filename_for_display (q
.symbol
->symtab ());
6837 /* Hold the option values for the 'info module .....' sub-commands. */
6839 struct info_modules_var_func_options
6842 std::string type_regexp
;
6843 std::string module_regexp
;
6846 /* The options used by 'info module variables' and 'info module functions'
6849 static const gdb::option::option_def info_modules_var_func_options_defs
[] = {
6850 gdb::option::boolean_option_def
<info_modules_var_func_options
> {
6852 [] (info_modules_var_func_options
*opt
) { return &opt
->quiet
; },
6853 nullptr, /* show_cmd_cb */
6854 nullptr /* set_doc */
6857 gdb::option::string_option_def
<info_modules_var_func_options
> {
6859 [] (info_modules_var_func_options
*opt
) { return &opt
->type_regexp
; },
6860 nullptr, /* show_cmd_cb */
6861 nullptr /* set_doc */
6864 gdb::option::string_option_def
<info_modules_var_func_options
> {
6866 [] (info_modules_var_func_options
*opt
) { return &opt
->module_regexp
; },
6867 nullptr, /* show_cmd_cb */
6868 nullptr /* set_doc */
6872 /* Return the option group used by the 'info module ...' sub-commands. */
6874 static inline gdb::option::option_def_group
6875 make_info_modules_var_func_options_def_group
6876 (info_modules_var_func_options
*opts
)
6878 return {{info_modules_var_func_options_defs
}, opts
};
6881 /* Implements the 'info module functions' command. */
6884 info_module_functions_command (const char *args
, int from_tty
)
6886 info_modules_var_func_options opts
;
6887 auto grp
= make_info_modules_var_func_options_def_group (&opts
);
6888 gdb::option::process_options
6889 (&args
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, grp
);
6890 if (args
!= nullptr && *args
== '\0')
6893 info_module_subcommand
6895 opts
.module_regexp
.empty () ? nullptr : opts
.module_regexp
.c_str (), args
,
6896 opts
.type_regexp
.empty () ? nullptr : opts
.type_regexp
.c_str (),
6897 SEARCH_FUNCTION_DOMAIN
);
6900 /* Implements the 'info module variables' command. */
6903 info_module_variables_command (const char *args
, int from_tty
)
6905 info_modules_var_func_options opts
;
6906 auto grp
= make_info_modules_var_func_options_def_group (&opts
);
6907 gdb::option::process_options
6908 (&args
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, grp
);
6909 if (args
!= nullptr && *args
== '\0')
6912 info_module_subcommand
6914 opts
.module_regexp
.empty () ? nullptr : opts
.module_regexp
.c_str (), args
,
6915 opts
.type_regexp
.empty () ? nullptr : opts
.type_regexp
.c_str (),
6919 /* Command completer for 'info module ...' sub-commands. */
6922 info_module_var_func_command_completer (struct cmd_list_element
*ignore
,
6923 completion_tracker
&tracker
,
6925 const char * /* word */)
6928 const auto group
= make_info_modules_var_func_options_def_group (nullptr);
6929 if (gdb::option::complete_options
6930 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
6933 const char *word
= advance_to_expression_complete_word_point (tracker
, text
);
6934 symbol_completer (ignore
, tracker
, text
, word
);
6939 INIT_GDB_FILE (symtab
)
6941 cmd_list_element
*c
;
6943 initialize_ordinary_address_classes ();
6945 c
= add_info ("variables", info_variables_command
,
6946 info_print_args_help (_("\
6947 All global and static variable names or those matching REGEXPs.\n\
6948 Usage: info variables [-q] [-n] [-t TYPEREGEXP] [NAMEREGEXP]\n\
6949 Prints the global and static variables.\n"),
6950 _("global and static variables"),
6952 set_cmd_completer_handle_brkchars (c
, info_vars_funcs_command_completer
);
6954 c
= add_info ("functions", info_functions_command
,
6955 info_print_args_help (_("\
6956 All function names or those matching REGEXPs.\n\
6957 Usage: info functions [-q] [-n] [-t TYPEREGEXP] [NAMEREGEXP]\n\
6958 Prints the functions.\n"),
6961 set_cmd_completer_handle_brkchars (c
, info_vars_funcs_command_completer
);
6963 c
= add_info ("types", info_types_command
, _("\
6964 All type names, or those matching REGEXP.\n\
6965 Usage: info types [-q] [REGEXP]\n\
6966 Print information about all types matching REGEXP, or all types if no\n\
6967 REGEXP is given. The optional flag -q disables printing of headers."));
6968 set_cmd_completer_handle_brkchars (c
, info_types_command_completer
);
6970 const auto info_sources_opts
6971 = make_info_sources_options_def_group (nullptr);
6973 static std::string info_sources_help
6974 = gdb::option::build_help (_("\
6975 All source files in the program or those matching REGEXP.\n\
6976 Usage: info sources [OPTION]... [REGEXP]\n\
6977 By default, REGEXP is used to match anywhere in the filename.\n\
6983 c
= add_info ("sources", info_sources_command
, info_sources_help
.c_str ());
6984 set_cmd_completer_handle_brkchars (c
, info_sources_command_completer
);
6986 c
= add_info ("modules", info_modules_command
,
6987 _("All module names, or those matching REGEXP."));
6988 set_cmd_completer_handle_brkchars (c
, info_types_command_completer
);
6990 add_info ("main", info_main_command
,
6991 _("Get main symbol to identify entry point into program."));
6993 add_basic_prefix_cmd ("module", class_info
, _("\
6994 Print information about modules."),
6995 &info_module_cmdlist
, 0, &infolist
);
6997 c
= add_cmd ("functions", class_info
, info_module_functions_command
, _("\
6998 Display functions arranged by modules.\n\
6999 Usage: info module functions [-q] [-m MODREGEXP] [-t TYPEREGEXP] [REGEXP]\n\
7000 Print a summary of all functions within each Fortran module, grouped by\n\
7001 module and file. For each function the line on which the function is\n\
7002 defined is given along with the type signature and name of the function.\n\
7004 If REGEXP is provided then only functions whose name matches REGEXP are\n\
7005 listed. If MODREGEXP is provided then only functions in modules matching\n\
7006 MODREGEXP are listed. If TYPEREGEXP is given then only functions whose\n\
7007 type signature matches TYPEREGEXP are listed.\n\
7009 The -q flag suppresses printing some header information."),
7010 &info_module_cmdlist
);
7011 set_cmd_completer_handle_brkchars
7012 (c
, info_module_var_func_command_completer
);
7014 c
= add_cmd ("variables", class_info
, info_module_variables_command
, _("\
7015 Display variables arranged by modules.\n\
7016 Usage: info module variables [-q] [-m MODREGEXP] [-t TYPEREGEXP] [REGEXP]\n\
7017 Print a summary of all variables within each Fortran module, grouped by\n\
7018 module and file. For each variable the line on which the variable is\n\
7019 defined is given along with the type and name of the variable.\n\
7021 If REGEXP is provided then only variables whose name matches REGEXP are\n\
7022 listed. If MODREGEXP is provided then only variables in modules matching\n\
7023 MODREGEXP are listed. If TYPEREGEXP is given then only variables whose\n\
7024 type matches TYPEREGEXP are listed.\n\
7026 The -q flag suppresses printing some header information."),
7027 &info_module_cmdlist
);
7028 set_cmd_completer_handle_brkchars
7029 (c
, info_module_var_func_command_completer
);
7031 add_com ("rbreak", class_breakpoint
, rbreak_command
,
7032 _("Set a breakpoint for all functions matching REGEXP."));
7034 add_setshow_enum_cmd ("multiple-symbols", no_class
,
7035 multiple_symbols_modes
, &multiple_symbols_mode
,
7037 Set how the debugger handles ambiguities in expressions."), _("\
7038 Show how the debugger handles ambiguities in expressions."), _("\
7039 Valid values are \"ask\", \"all\", \"cancel\", and the default is \"all\"."),
7040 NULL
, NULL
, &setlist
, &showlist
);
7042 add_setshow_boolean_cmd ("basenames-may-differ", class_obscure
,
7043 &basenames_may_differ
, _("\
7044 Set whether a source file may have multiple base names."), _("\
7045 Show whether a source file may have multiple base names."), _("\
7046 (A \"base name\" is the name of a file with the directory part removed.\n\
7047 Example: The base name of \"/home/user/hello.c\" is \"hello.c\".)\n\
7048 If set, GDB will canonicalize file names (e.g., expand symlinks)\n\
7049 before comparing them. Canonicalization is an expensive operation,\n\
7050 but it allows the same file be known by more than one base name.\n\
7051 If not set (the default), all source files are assumed to have just\n\
7052 one base name, and gdb will do file name comparisons more efficiently."),
7054 &setlist
, &showlist
);
7056 add_setshow_zuinteger_cmd ("symtab-create", no_class
, &symtab_create_debug
,
7057 _("Set debugging of symbol table creation."),
7058 _("Show debugging of symbol table creation."), _("\
7059 When enabled (non-zero), debugging messages are printed when building\n\
7060 symbol tables. A value of 1 (one) normally provides enough information.\n\
7061 A value greater than 1 provides more verbose information."),
7064 &setdebuglist
, &showdebuglist
);
7066 add_setshow_zuinteger_cmd ("symbol-lookup", no_class
, &symbol_lookup_debug
,
7068 Set debugging of symbol lookup."), _("\
7069 Show debugging of symbol lookup."), _("\
7070 When enabled (non-zero), symbol lookups are logged."),
7072 &setdebuglist
, &showdebuglist
);
7074 add_setshow_zuinteger_cmd ("symbol-cache-size", no_class
,
7075 &new_symbol_cache_size
,
7076 _("Set the size of the symbol cache."),
7077 _("Show the size of the symbol cache."), _("\
7078 The size of the symbol cache.\n\
7079 If zero then the symbol cache is disabled."),
7080 set_symbol_cache_size_handler
, NULL
,
7081 &maintenance_set_cmdlist
,
7082 &maintenance_show_cmdlist
);
7084 add_setshow_boolean_cmd ("ignore-prologue-end-flag", no_class
,
7085 &ignore_prologue_end_flag
,
7086 _("Set if the PROLOGUE-END flag is ignored."),
7087 _("Show if the PROLOGUE-END flag is ignored."),
7089 The PROLOGUE-END flag from the line-table entries is used to place\n\
7090 breakpoints past the prologue of functions. Disabling its use forces\n\
7091 the use of prologue scanners."),
7093 &maintenance_set_cmdlist
,
7094 &maintenance_show_cmdlist
);
7097 add_cmd ("symbol-cache", class_maintenance
, maintenance_print_symbol_cache
,
7098 _("Dump the symbol cache for each program space."),
7099 &maintenanceprintlist
);
7101 add_cmd ("symbol-cache-statistics", class_maintenance
,
7102 maintenance_print_symbol_cache_statistics
,
7103 _("Print symbol cache statistics for each program space."),
7104 &maintenanceprintlist
);
7106 cmd_list_element
*maintenance_flush_symbol_cache_cmd
7107 = add_cmd ("symbol-cache", class_maintenance
,
7108 maintenance_flush_symbol_cache
,
7109 _("Flush the symbol cache for each program space."),
7110 &maintenanceflushlist
);
7111 c
= add_alias_cmd ("flush-symbol-cache", maintenance_flush_symbol_cache_cmd
,
7112 class_maintenance
, 0, &maintenancelist
);
7113 deprecate_cmd (c
, "maintenance flush symbol-cache");
7115 gdb::observers::new_objfile
.attach (symtab_new_objfile_observer
, "symtab");
7116 gdb::observers::all_objfiles_removed
.attach (symtab_all_objfiles_removed
,
7118 gdb::observers::free_objfile
.attach (symtab_free_objfile_observer
, "symtab");