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>
61 #include "cp-support.h"
62 #include "observable.h"
64 #include "macroscope.h"
66 #include "parser-defs.h"
67 #include "completer.h"
68 #include "progspace-and-thread.h"
70 #include "filename-seen-cache.h"
71 #include "arch-utils.h"
73 #include <string_view>
74 #include "gdbsupport/pathstuff.h"
75 #include "gdbsupport/common-utils.h"
77 #include "gdbsupport/unordered_set.h"
79 /* Forward declarations for local functions. */
81 static void rbreak_command (const char *, int);
83 static int find_line_common (const linetable
*, int, int *, int);
85 static struct block_symbol
86 lookup_symbol_aux (const char *name
,
87 symbol_name_match_type match_type
,
88 const struct block
*block
,
89 const domain_search_flags domain
,
90 enum language language
,
91 struct field_of_this_result
*);
94 struct block_symbol
lookup_local_symbol (const char *name
,
95 symbol_name_match_type match_type
,
96 const struct block
*block
,
97 const domain_search_flags domain
,
98 const struct language_defn
*langdef
);
100 static struct block_symbol
101 lookup_symbol_in_objfile (struct objfile
*objfile
,
102 enum block_enum block_index
,
104 const domain_search_flags domain
);
106 static void set_main_name (program_space
*pspace
, const char *name
,
109 /* Type of the data stored on the program space. */
113 /* Name of "main". */
115 std::string name_of_main
;
117 /* Language of "main". */
119 enum language language_of_main
= language_unknown
;
122 /* Program space key for finding name and language of "main". */
124 static const registry
<program_space
>::key
<main_info
> main_progspace_key
;
126 /* Symbol lookup is not reentrant (though this is not an intrinsic
127 restriction). Keep track of whether a symbol lookup is active, to be able
128 to detect reentrancy. */
129 static bool in_symbol_lookup
;
131 /* Struct to mark that a symbol lookup is active for the duration of its
134 struct enter_symbol_lookup
136 enter_symbol_lookup ()
138 /* Ensure that the current language has been set. Normally the
139 language is set lazily. However, when performing a symbol lookup,
140 this could result in a recursive call into the lookup code in some
141 cases. Set it now to ensure that this does not happen. */
142 get_current_language ();
144 /* Detect symbol lookup reentrance. */
145 gdb_assert (!in_symbol_lookup
);
147 in_symbol_lookup
= true;
150 ~enter_symbol_lookup ()
153 gdb_assert (in_symbol_lookup
);
155 in_symbol_lookup
= false;
158 DISABLE_COPY_AND_ASSIGN (enter_symbol_lookup
);
161 /* The default symbol cache size.
162 There is no extra cpu cost for large N (except when flushing the cache,
163 which is rare). The value here is just a first attempt. A better default
164 value may be higher or lower. A prime number can make up for a bad hash
165 computation, so that's why the number is what it is. */
166 #define DEFAULT_SYMBOL_CACHE_SIZE 1021
168 /* The maximum symbol cache size.
169 There's no method to the decision of what value to use here, other than
170 there's no point in allowing a user typo to make gdb consume all memory. */
171 #define MAX_SYMBOL_CACHE_SIZE (1024*1024)
173 /* symbol_cache_lookup returns this if a previous lookup failed to find the
174 symbol in any objfile. */
175 #define SYMBOL_LOOKUP_FAILED \
176 ((struct block_symbol) {(struct symbol *) 1, NULL})
177 #define SYMBOL_LOOKUP_FAILED_P(SIB) (SIB.symbol == (struct symbol *) 1)
179 /* Recording lookups that don't find the symbol is just as important, if not
180 more so, than recording found symbols. */
182 enum symbol_cache_slot_state
185 SYMBOL_SLOT_NOT_FOUND
,
189 struct symbol_cache_slot
191 enum symbol_cache_slot_state state
;
193 /* The objfile that was current when the symbol was looked up.
194 This is only needed for global blocks, but for simplicity's sake
195 we allocate the space for both. If data shows the extra space used
196 for static blocks is a problem, we can split things up then.
198 Global blocks need cache lookup to include the objfile context because
199 we need to account for gdbarch_iterate_over_objfiles_in_search_order
200 which can traverse objfiles in, effectively, any order, depending on
201 the current objfile, thus affecting which symbol is found. Normally,
202 only the current objfile is searched first, and then the rest are
203 searched in recorded order; but putting cache lookup inside
204 gdbarch_iterate_over_objfiles_in_search_order would be awkward.
205 Instead we just make the current objfile part of the context of
206 cache lookup. This means we can record the same symbol multiple times,
207 each with a different "current objfile" that was in effect when the
208 lookup was saved in the cache, but cache space is pretty cheap. */
209 const struct objfile
*objfile_context
;
211 /* The domain that was searched for initially. This must exactly
213 domain_search_flags domain
;
217 struct block_symbol found
;
222 /* Clear out SLOT. */
225 symbol_cache_clear_slot (struct symbol_cache_slot
*slot
)
227 if (slot
->state
== SYMBOL_SLOT_NOT_FOUND
)
228 xfree (slot
->value
.name
);
229 slot
->state
= SYMBOL_SLOT_UNUSED
;
232 /* Symbols don't specify global vs static block.
233 So keep them in separate caches. */
235 struct block_symbol_cache
239 unsigned int collisions
;
241 /* SYMBOLS is a variable length array of this size.
242 One can imagine that in general one cache (global/static) should be a
243 fraction of the size of the other, but there's no data at the moment
244 on which to decide. */
247 struct symbol_cache_slot symbols
[1];
250 /* Clear all slots of BSC and free BSC. */
253 destroy_block_symbol_cache (struct block_symbol_cache
*bsc
)
257 for (unsigned int i
= 0; i
< bsc
->size
; i
++)
258 symbol_cache_clear_slot (&bsc
->symbols
[i
]);
265 Searching for symbols in the static and global blocks over multiple objfiles
266 again and again can be slow, as can searching very big objfiles. This is a
267 simple cache to improve symbol lookup performance, which is critical to
268 overall gdb performance.
270 Symbols are hashed on the name, its domain, and block.
271 They are also hashed on their objfile for objfile-specific lookups. */
275 symbol_cache () = default;
279 destroy_block_symbol_cache (global_symbols
);
280 destroy_block_symbol_cache (static_symbols
);
283 struct block_symbol_cache
*global_symbols
= nullptr;
284 struct block_symbol_cache
*static_symbols
= nullptr;
287 /* Program space key for finding its symbol cache. */
289 static const registry
<program_space
>::key
<symbol_cache
> symbol_cache_key
;
291 /* When non-zero, print debugging messages related to symtab creation. */
292 unsigned int symtab_create_debug
= 0;
294 /* When non-zero, print debugging messages related to symbol lookup. */
295 unsigned int symbol_lookup_debug
= 0;
297 /* The size of the cache is staged here. */
298 static unsigned int new_symbol_cache_size
= DEFAULT_SYMBOL_CACHE_SIZE
;
300 /* The current value of the symbol cache size.
301 This is saved so that if the user enters a value too big we can restore
302 the original value from here. */
303 static unsigned int symbol_cache_size
= DEFAULT_SYMBOL_CACHE_SIZE
;
305 /* True if a file may be known by two different basenames.
306 This is the uncommon case, and significantly slows down gdb.
307 Default set to "off" to not slow down the common case. */
308 bool basenames_may_differ
= false;
310 /* Allow the user to configure the debugger behavior with respect
311 to multiple-choice menus when more than one symbol matches during
314 const char multiple_symbols_ask
[] = "ask";
315 const char multiple_symbols_all
[] = "all";
316 const char multiple_symbols_cancel
[] = "cancel";
317 static const char *const multiple_symbols_modes
[] =
319 multiple_symbols_ask
,
320 multiple_symbols_all
,
321 multiple_symbols_cancel
,
324 static const char *multiple_symbols_mode
= multiple_symbols_all
;
326 /* When TRUE, ignore the prologue-end flag in linetable_entry when searching
327 for the SAL past a function prologue. */
328 static bool ignore_prologue_end_flag
= false;
330 /* Read-only accessor to AUTO_SELECT_MODE. */
333 multiple_symbols_select_mode (void)
335 return multiple_symbols_mode
;
338 /* Return the name of a domain_enum. */
341 domain_name (domain_enum e
)
345 #define SYM_DOMAIN(X) \
346 case X ## _DOMAIN: return #X "_DOMAIN";
347 #include "sym-domains.def"
349 default: gdb_assert_not_reached ("bad domain_enum");
356 domain_name (domain_search_flags flags
)
358 static constexpr domain_search_flags::string_mapping mapping
[] = {
359 #define SYM_DOMAIN(X) \
360 MAP_ENUM_FLAG (SEARCH_ ## X ## _DOMAIN),
361 #include "sym-domains.def"
365 return flags
.to_string (mapping
);
371 from_scripting_domain (int val
)
373 if ((val
& SCRIPTING_SEARCH_FLAG
) == 0)
375 /* VAL should be one of the domain constants. Verify this and
376 convert it to a search constant. */
379 #define SYM_DOMAIN(X) \
380 case X ## _DOMAIN: break;
381 #include "sym-domains.def"
384 error (_("unrecognized domain constant"));
386 domain_search_flags result
= to_search_flags ((domain_enum
) val
);
387 if (val
== VAR_DOMAIN
)
389 /* This matches the historical practice. */
390 result
|= SEARCH_TYPE_DOMAIN
| SEARCH_FUNCTION_DOMAIN
;
396 /* VAL is several search constants or'd together. Verify
398 val
&= ~SCRIPTING_SEARCH_FLAG
;
400 #define SYM_DOMAIN(X) \
401 check &= ~ (int) SEARCH_ ## X ## _DOMAIN;
402 #include "sym-domains.def"
405 error (_("unrecognized domain constant"));
406 return (domain_search_flag
) val
;
413 search_symbol_list (const char *name
, int num
, struct symbol
**syms
)
415 for (int i
= 0; i
< num
; ++i
)
417 if (strcmp (name
, syms
[i
]->natural_name ()) == 0)
426 linetable_entry::pc (const struct objfile
*objfile
) const
428 return CORE_ADDR (m_pc
) + objfile
->text_section_offset ();
434 compunit_symtab::find_call_site (CORE_ADDR pc
) const
436 if (m_call_site_htab
== nullptr)
439 CORE_ADDR delta
= this->objfile ()->text_section_offset ();
441 if (auto it
= m_call_site_htab
->find (static_cast<unrelocated_addr
> (pc
- delta
));
442 it
!= m_call_site_htab
->end ())
445 /* See if the arch knows another PC we should try. On some
446 platforms, GCC emits a DWARF call site that is offset from the
447 actual return location. */
448 struct gdbarch
*arch
= objfile ()->arch ();
449 CORE_ADDR new_pc
= gdbarch_update_call_site_pc (arch
, pc
);
454 if (auto it
= m_call_site_htab
->find (static_cast<unrelocated_addr
> (new_pc
- delta
));
455 it
!= m_call_site_htab
->end ())
464 compunit_symtab::set_call_site_htab (call_site_htab_t
&&call_site_htab
)
466 gdb_assert (m_call_site_htab
== nullptr);
467 m_call_site_htab
= new call_site_htab_t (std::move (call_site_htab
));
473 compunit_symtab::set_primary_filetab (symtab
*primary_filetab
)
475 symtab
*prev_filetab
= nullptr;
477 /* Move PRIMARY_FILETAB to the head of the filetab list. */
478 for (symtab
*filetab
: this->filetabs ())
480 if (filetab
== primary_filetab
)
482 if (prev_filetab
!= nullptr)
484 prev_filetab
->next
= primary_filetab
->next
;
485 primary_filetab
->next
= m_filetabs
;
486 m_filetabs
= primary_filetab
;
492 prev_filetab
= filetab
;
495 gdb_assert (primary_filetab
== m_filetabs
);
501 compunit_symtab::primary_filetab () const
503 gdb_assert (m_filetabs
!= nullptr);
505 /* The primary file symtab is the first one in the list. */
512 compunit_symtab::language () const
514 struct symtab
*symtab
= primary_filetab ();
516 /* The language of the compunit symtab is the language of its
517 primary source file. */
518 return symtab
->language ();
524 compunit_symtab::forget_cached_source_info ()
526 for (symtab
*s
: filetabs ())
527 s
->release_fullname ();
533 compunit_symtab::finalize ()
535 this->forget_cached_source_info ();
536 delete m_call_site_htab
;
539 /* The relocated address of the minimal symbol, using the section
540 offsets from OBJFILE. */
543 minimal_symbol::value_address (objfile
*objfile
) const
545 if (this->maybe_copied (objfile
))
546 return this->get_maybe_copied_address (objfile
);
548 return (CORE_ADDR (this->unrelocated_address ())
549 + objfile
->section_offsets
[this->section_index ()]);
555 minimal_symbol::data_p () const
557 return m_type
== mst_data
560 || m_type
== mst_file_data
561 || m_type
== mst_file_bss
;
567 minimal_symbol::text_p () const
569 return m_type
== mst_text
570 || m_type
== mst_text_gnu_ifunc
571 || m_type
== mst_data_gnu_ifunc
572 || m_type
== mst_slot_got_plt
573 || m_type
== mst_solib_trampoline
574 || m_type
== mst_file_text
;
580 minimal_symbol::maybe_copied (objfile
*objfile
) const
582 return (objfile
->object_format_has_copy_relocs
583 && (objfile
->flags
& OBJF_MAINLINE
) == 0
584 && (m_type
== mst_data
|| m_type
== mst_bss
));
587 /* See whether FILENAME matches SEARCH_NAME using the rule that we
588 advertise to the user. (The manual's description of linespecs
589 describes what we advertise). Returns true if they match, false
593 compare_filenames_for_search (const char *filename
, const char *search_name
)
595 int len
= strlen (filename
);
596 size_t search_len
= strlen (search_name
);
598 if (len
< search_len
)
601 /* The tail of FILENAME must match. */
602 if (FILENAME_CMP (filename
+ len
- search_len
, search_name
) != 0)
605 /* Either the names must completely match, or the character
606 preceding the trailing SEARCH_NAME segment of FILENAME must be a
609 The check !IS_ABSOLUTE_PATH ensures SEARCH_NAME "/dir/file.c"
610 cannot match FILENAME "/path//dir/file.c" - as user has requested
611 absolute path. The sama applies for "c:\file.c" possibly
612 incorrectly hypothetically matching "d:\dir\c:\file.c".
614 The HAS_DRIVE_SPEC purpose is to make FILENAME "c:file.c"
615 compatible with SEARCH_NAME "file.c". In such case a compiler had
616 to put the "c:file.c" name into debug info. Such compatibility
617 works only on GDB built for DOS host. */
618 return (len
== search_len
619 || (!IS_ABSOLUTE_PATH (search_name
)
620 && IS_DIR_SEPARATOR (filename
[len
- search_len
- 1]))
621 || (HAS_DRIVE_SPEC (filename
)
622 && STRIP_DRIVE_SPEC (filename
) == &filename
[len
- search_len
]));
625 /* Check for a symtab of a specific name by searching some symtabs.
626 This is a helper function for callbacks of iterate_over_symtabs.
628 If NAME is not absolute, then REAL_PATH is NULL
629 If NAME is absolute, then REAL_PATH is the gdb_realpath form of NAME.
631 The return value, NAME, REAL_PATH and CALLBACK are identical to the
632 `map_symtabs_matching_filename' method of quick_symbol_functions.
634 FIRST and AFTER_LAST indicate the range of compunit symtabs to search.
635 Each symtab within the specified compunit symtab is also searched.
636 AFTER_LAST is one past the last compunit symtab to search; NULL means to
637 search until the end of the list. */
640 iterate_over_some_symtabs (const char *name
,
641 const char *real_path
,
642 struct compunit_symtab
*first
,
643 struct compunit_symtab
*after_last
,
644 gdb::function_view
<bool (symtab
*)> callback
)
646 struct compunit_symtab
*cust
;
647 const char* base_name
= lbasename (name
);
649 for (cust
= first
; cust
!= NULL
&& cust
!= after_last
; cust
= cust
->next
)
651 /* Skip included compunits. */
652 if (cust
->user
!= nullptr)
655 for (symtab
*s
: cust
->filetabs ())
657 if (compare_filenames_for_search (s
->filename
, name
))
664 /* Before we invoke realpath, which can get expensive when many
665 files are involved, do a quick comparison of the basenames. */
666 if (! basenames_may_differ
667 && FILENAME_CMP (base_name
, lbasename (s
->filename
)) != 0)
670 if (compare_filenames_for_search (symtab_to_fullname (s
), name
))
677 /* If the user gave us an absolute path, try to find the file in
678 this symtab and use its absolute path. */
679 if (real_path
!= NULL
)
681 const char *fullname
= symtab_to_fullname (s
);
683 gdb_assert (IS_ABSOLUTE_PATH (real_path
));
684 gdb_assert (IS_ABSOLUTE_PATH (name
));
685 gdb::unique_xmalloc_ptr
<char> fullname_real_path
686 = gdb_realpath (fullname
);
687 fullname
= fullname_real_path
.get ();
688 if (FILENAME_CMP (real_path
, fullname
) == 0)
704 iterate_over_symtabs (program_space
*pspace
, const char *name
,
705 gdb::function_view
<bool (symtab
*)> callback
)
707 gdb::unique_xmalloc_ptr
<char> real_path
;
709 /* Here we are interested in canonicalizing an absolute path, not
710 absolutizing a relative path. */
711 if (IS_ABSOLUTE_PATH (name
))
713 real_path
= gdb_realpath (name
);
714 gdb_assert (IS_ABSOLUTE_PATH (real_path
.get ()));
717 for (objfile
*objfile
: pspace
->objfiles ())
718 if (iterate_over_some_symtabs (name
, real_path
.get (),
719 objfile
->compunit_symtabs
, nullptr,
723 /* Same search rules as above apply here, but now we look through the
725 for (objfile
*objfile
: pspace
->objfiles ())
726 if (objfile
->map_symtabs_matching_filename (name
, real_path
.get (),
734 lookup_symtab (program_space
*pspace
, const char *name
)
736 struct symtab
*result
= NULL
;
738 iterate_over_symtabs (pspace
, name
, [&] (symtab
*symtab
)
748 /* Mangle a GDB method stub type. This actually reassembles the pieces of the
749 full method name, which consist of the class name (from T), the unadorned
750 method name from METHOD_ID, and the signature for the specific overload,
751 specified by SIGNATURE_ID. Note that this function is g++ specific. */
754 gdb_mangle_name (struct type
*type
, int method_id
, int signature_id
)
756 int mangled_name_len
;
758 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, method_id
);
759 struct fn_field
*method
= &f
[signature_id
];
760 const char *field_name
= TYPE_FN_FIELDLIST_NAME (type
, method_id
);
761 const char *physname
= TYPE_FN_FIELD_PHYSNAME (f
, signature_id
);
762 const char *newname
= type
->name ();
764 /* Does the form of physname indicate that it is the full mangled name
765 of a constructor (not just the args)? */
766 int is_full_physname_constructor
;
769 int is_destructor
= is_destructor_name (physname
);
770 /* Need a new type prefix. */
771 const char *const_prefix
= method
->is_const
? "C" : "";
772 const char *volatile_prefix
= method
->is_volatile
? "V" : "";
774 int len
= (newname
== NULL
? 0 : strlen (newname
));
776 /* Nothing to do if physname already contains a fully mangled v3 abi name
777 or an operator name. */
778 if ((physname
[0] == '_' && physname
[1] == 'Z')
779 || is_operator_name (field_name
))
780 return xstrdup (physname
);
782 is_full_physname_constructor
= is_constructor_name (physname
);
784 is_constructor
= is_full_physname_constructor
785 || (newname
&& strcmp (field_name
, newname
) == 0);
788 is_destructor
= (startswith (physname
, "__dt"));
790 if (is_destructor
|| is_full_physname_constructor
)
792 mangled_name
= (char *) xmalloc (strlen (physname
) + 1);
793 strcpy (mangled_name
, physname
);
799 xsnprintf (buf
, sizeof (buf
), "__%s%s", const_prefix
, volatile_prefix
);
801 else if (physname
[0] == 't' || physname
[0] == 'Q')
803 /* The physname for template and qualified methods already includes
805 xsnprintf (buf
, sizeof (buf
), "__%s%s", const_prefix
, volatile_prefix
);
811 xsnprintf (buf
, sizeof (buf
), "__%s%s%d", const_prefix
,
812 volatile_prefix
, len
);
814 mangled_name_len
= ((is_constructor
? 0 : strlen (field_name
))
815 + strlen (buf
) + len
+ strlen (physname
) + 1);
817 mangled_name
= (char *) xmalloc (mangled_name_len
);
819 mangled_name
[0] = '\0';
821 strcpy (mangled_name
, field_name
);
823 strcat (mangled_name
, buf
);
824 /* If the class doesn't have a name, i.e. newname NULL, then we just
825 mangle it using 0 for the length of the class. Thus it gets mangled
826 as something starting with `::' rather than `classname::'. */
828 strcat (mangled_name
, newname
);
830 strcat (mangled_name
, physname
);
831 return (mangled_name
);
837 general_symbol_info::set_demangled_name (const char *name
,
838 struct obstack
*obstack
)
840 if (language () == language_ada
)
845 language_specific
.obstack
= obstack
;
850 language_specific
.demangled_name
= name
;
854 language_specific
.demangled_name
= name
;
858 /* Initialize the language dependent portion of a symbol
859 depending upon the language for the symbol. */
862 general_symbol_info::set_language (enum language language
,
863 struct obstack
*obstack
)
865 m_language
= language
;
866 if (language
== language_cplus
867 || language
== language_d
868 || language
== language_go
869 || language
== language_objc
870 || language
== language_fortran
)
872 set_demangled_name (NULL
, obstack
);
874 else if (language
== language_ada
)
876 gdb_assert (ada_mangled
== 0);
877 language_specific
.obstack
= obstack
;
881 memset (&language_specific
, 0, sizeof (language_specific
));
885 /* Functions to initialize a symbol's mangled name. */
887 /* Objects of this type are stored in the demangled name hash table. */
888 struct demangled_name_entry
890 demangled_name_entry (std::string_view mangled_name
)
891 : mangled (mangled_name
) {}
893 std::string_view mangled
;
894 enum language language
;
895 gdb::unique_xmalloc_ptr
<char> demangled
;
898 /* Hash function for the demangled name hash. */
901 hash_demangled_name_entry (const void *data
)
903 const struct demangled_name_entry
*e
904 = (const struct demangled_name_entry
*) data
;
906 return gdb::string_view_hash () (e
->mangled
);
909 /* Equality function for the demangled name hash. */
912 eq_demangled_name_entry (const void *a
, const void *b
)
914 const struct demangled_name_entry
*da
915 = (const struct demangled_name_entry
*) a
;
916 const struct demangled_name_entry
*db
917 = (const struct demangled_name_entry
*) b
;
919 return da
->mangled
== db
->mangled
;
923 free_demangled_name_entry (void *data
)
925 struct demangled_name_entry
*e
926 = (struct demangled_name_entry
*) data
;
928 e
->~demangled_name_entry();
931 /* Create the hash table used for demangled names. Each hash entry is
932 a pair of strings; one for the mangled name and one for the demangled
933 name. The entry is hashed via just the mangled name. */
936 create_demangled_names_hash (struct objfile_per_bfd_storage
*per_bfd
)
938 /* Choose 256 as the starting size of the hash table, somewhat arbitrarily.
939 The hash table code will round this up to the next prime number.
940 Choosing a much larger table size wastes memory, and saves only about
941 1% in symbol reading. However, if the minsym count is already
942 initialized (e.g. because symbol name setting was deferred to
943 a background thread) we can initialize the hashtable with a count
944 based on that, because we will almost certainly have at least that
945 many entries. If we have a nonzero number but less than 256,
946 we still stay with 256 to have some space for psymbols, etc. */
948 /* htab will expand the table when it is 3/4th full, so we account for that
949 here. +2 to round up. */
950 int minsym_based_count
= (per_bfd
->minimal_symbol_count
+ 2) / 3 * 4;
951 int count
= std::max (per_bfd
->minimal_symbol_count
, minsym_based_count
);
953 per_bfd
->demangled_names_hash
.reset (htab_create_alloc
954 (count
, hash_demangled_name_entry
, eq_demangled_name_entry
,
955 free_demangled_name_entry
, xcalloc
, xfree
));
960 gdb::unique_xmalloc_ptr
<char>
961 symbol_find_demangled_name (struct general_symbol_info
*gsymbol
,
964 gdb::unique_xmalloc_ptr
<char> demangled
;
967 if (gsymbol
->language () != language_unknown
)
969 const struct language_defn
*lang
= language_def (gsymbol
->language ());
971 lang
->sniff_from_mangled_name (mangled
, &demangled
);
975 for (i
= language_unknown
; i
< nr_languages
; ++i
)
977 enum language l
= (enum language
) i
;
978 const struct language_defn
*lang
= language_def (l
);
980 if (lang
->sniff_from_mangled_name (mangled
, &demangled
))
982 gsymbol
->m_language
= l
;
990 /* Set both the mangled and demangled (if any) names for GSYMBOL based
991 on LINKAGE_NAME and LEN. Ordinarily, NAME is copied onto the
992 objfile's obstack; but if COPY_NAME is 0 and if NAME is
993 NUL-terminated, then this function assumes that NAME is already
994 correctly saved (either permanently or with a lifetime tied to the
995 objfile), and it will not be copied.
997 The hash table corresponding to OBJFILE is used, and the memory
998 comes from the per-BFD storage_obstack. LINKAGE_NAME is copied,
999 so the pointer can be discarded after calling this function. */
1002 general_symbol_info::compute_and_set_names (std::string_view linkage_name
,
1004 objfile_per_bfd_storage
*per_bfd
,
1005 std::optional
<hashval_t
> hash
)
1007 struct demangled_name_entry
**slot
;
1009 if (language () == language_ada
)
1011 /* In Ada, we do the symbol lookups using the mangled name, so
1012 we can save some space by not storing the demangled name. */
1014 m_name
= linkage_name
.data ();
1016 m_name
= obstack_strndup (&per_bfd
->storage_obstack
,
1017 linkage_name
.data (),
1018 linkage_name
.length ());
1019 set_demangled_name (NULL
, &per_bfd
->storage_obstack
);
1024 if (per_bfd
->demangled_names_hash
== NULL
)
1025 create_demangled_names_hash (per_bfd
);
1027 struct demangled_name_entry
entry (linkage_name
);
1028 if (!hash
.has_value ())
1029 hash
= hash_demangled_name_entry (&entry
);
1030 slot
= ((struct demangled_name_entry
**)
1031 htab_find_slot_with_hash (per_bfd
->demangled_names_hash
.get (),
1032 &entry
, *hash
, INSERT
));
1034 /* The const_cast is safe because the only reason it is already
1035 initialized is if we purposefully set it from a background
1036 thread to avoid doing the work here. However, it is still
1037 allocated from the heap and needs to be freed by us, just
1038 like if we called symbol_find_demangled_name here. If this is
1039 nullptr, we call symbol_find_demangled_name below, but we put
1040 this smart pointer here to be sure that we don't leak this name. */
1041 gdb::unique_xmalloc_ptr
<char> demangled_name
1042 (const_cast<char *> (language_specific
.demangled_name
));
1044 /* If this name is not in the hash table, add it. */
1046 /* A C version of the symbol may have already snuck into the table.
1047 This happens to, e.g., main.init (__go_init_main). Cope. */
1048 || (language () == language_go
&& (*slot
)->demangled
== nullptr))
1050 /* A 0-terminated copy of the linkage name. Callers must set COPY_NAME
1051 to true if the string might not be nullterminated. We have to make
1052 this copy because demangling needs a nullterminated string. */
1053 std::string_view linkage_name_copy
;
1056 char *alloc_name
= (char *) alloca (linkage_name
.length () + 1);
1057 memcpy (alloc_name
, linkage_name
.data (), linkage_name
.length ());
1058 alloc_name
[linkage_name
.length ()] = '\0';
1060 linkage_name_copy
= std::string_view (alloc_name
,
1061 linkage_name
.length ());
1064 linkage_name_copy
= linkage_name
;
1066 if (demangled_name
.get () == nullptr)
1068 = symbol_find_demangled_name (this, linkage_name_copy
.data ());
1070 /* Suppose we have demangled_name==NULL, copy_name==0, and
1071 linkage_name_copy==linkage_name. In this case, we already have the
1072 mangled name saved, and we don't have a demangled name. So,
1073 you might think we could save a little space by not recording
1074 this in the hash table at all.
1076 It turns out that it is actually important to still save such
1077 an entry in the hash table, because storing this name gives
1078 us better bcache hit rates for partial symbols. */
1082 = ((struct demangled_name_entry
*)
1083 obstack_alloc (&per_bfd
->storage_obstack
,
1084 sizeof (demangled_name_entry
)));
1085 new (*slot
) demangled_name_entry (linkage_name
);
1089 /* If we must copy the mangled name, put it directly after
1090 the struct so we can have a single allocation. */
1092 = ((struct demangled_name_entry
*)
1093 obstack_alloc (&per_bfd
->storage_obstack
,
1094 sizeof (demangled_name_entry
)
1095 + linkage_name
.length () + 1));
1096 char *mangled_ptr
= reinterpret_cast<char *> (*slot
+ 1);
1097 memcpy (mangled_ptr
, linkage_name
.data (), linkage_name
.length ());
1098 mangled_ptr
[linkage_name
.length ()] = '\0';
1099 new (*slot
) demangled_name_entry
1100 (std::string_view (mangled_ptr
, linkage_name
.length ()));
1102 (*slot
)->demangled
= std::move (demangled_name
);
1103 (*slot
)->language
= language ();
1105 else if (language () == language_unknown
)
1106 m_language
= (*slot
)->language
;
1108 m_name
= (*slot
)->mangled
.data ();
1109 set_demangled_name ((*slot
)->demangled
.get (), &per_bfd
->storage_obstack
);
1115 general_symbol_info::natural_name () const
1117 switch (language ())
1119 case language_cplus
:
1123 case language_fortran
:
1125 if (language_specific
.demangled_name
!= nullptr)
1126 return language_specific
.demangled_name
;
1129 return ada_decode_symbol (this);
1133 return linkage_name ();
1139 general_symbol_info::demangled_name () const
1141 const char *dem_name
= NULL
;
1143 switch (language ())
1145 case language_cplus
:
1149 case language_fortran
:
1151 dem_name
= language_specific
.demangled_name
;
1154 dem_name
= ada_decode_symbol (this);
1165 general_symbol_info::search_name () const
1167 if (language () == language_ada
)
1168 return linkage_name ();
1170 return natural_name ();
1175 struct obj_section
*
1176 general_symbol_info::obj_section (const struct objfile
*objfile
) const
1178 if (section_index () >= 0)
1179 return &objfile
->sections_start
[section_index ()];
1186 symbol_matches_search_name (const struct general_symbol_info
*gsymbol
,
1187 const lookup_name_info
&name
)
1189 symbol_name_matcher_ftype
*name_match
1190 = language_def (gsymbol
->language ())->get_symbol_name_matcher (name
);
1191 return name_match (gsymbol
->search_name (), name
, NULL
);
1196 /* Return true if the two sections are the same, or if they could
1197 plausibly be copies of each other, one in an original object
1198 file and another in a separated debug file. */
1201 matching_obj_sections (struct obj_section
*obj_first
,
1202 struct obj_section
*obj_second
)
1204 asection
*first
= obj_first
? obj_first
->the_bfd_section
: NULL
;
1205 asection
*second
= obj_second
? obj_second
->the_bfd_section
: NULL
;
1207 /* If they're the same section, then they match. */
1208 if (first
== second
)
1211 /* If either is NULL, give up. */
1212 if (first
== NULL
|| second
== NULL
)
1215 /* This doesn't apply to absolute symbols. */
1216 if (first
->owner
== NULL
|| second
->owner
== NULL
)
1219 /* If they're in the same object file, they must be different sections. */
1220 if (first
->owner
== second
->owner
)
1223 /* Check whether the two sections are potentially corresponding. They must
1224 have the same size, address, and name. We can't compare section indexes,
1225 which would be more reliable, because some sections may have been
1227 if (bfd_section_size (first
) != bfd_section_size (second
))
1230 /* In-memory addresses may start at a different offset, relativize them. */
1231 if (bfd_section_vma (first
) - bfd_get_start_address (first
->owner
)
1232 != bfd_section_vma (second
) - bfd_get_start_address (second
->owner
))
1235 if (bfd_section_name (first
) == NULL
1236 || bfd_section_name (second
) == NULL
1237 || strcmp (bfd_section_name (first
), bfd_section_name (second
)) != 0)
1240 /* Otherwise check that they are in corresponding objfiles. */
1242 struct objfile
*obj
= NULL
;
1243 for (objfile
*objfile
: current_program_space
->objfiles ())
1244 if (objfile
->obfd
== first
->owner
)
1249 gdb_assert (obj
!= NULL
);
1251 if (obj
->separate_debug_objfile
!= NULL
1252 && obj
->separate_debug_objfile
->obfd
== second
->owner
)
1254 if (obj
->separate_debug_objfile_backlink
!= NULL
1255 && obj
->separate_debug_objfile_backlink
->obfd
== second
->owner
)
1261 /* Hash function for the symbol cache. */
1264 hash_symbol_entry (const struct objfile
*objfile_context
,
1265 const char *name
, domain_search_flags domain
)
1267 unsigned int hash
= (uintptr_t) objfile_context
;
1270 hash
+= htab_hash_string (name
);
1277 /* Equality function for the symbol cache. */
1280 eq_symbol_entry (const struct symbol_cache_slot
*slot
,
1281 const struct objfile
*objfile_context
,
1282 const char *name
, domain_search_flags domain
)
1284 const char *slot_name
;
1286 if (slot
->state
== SYMBOL_SLOT_UNUSED
)
1289 if (slot
->objfile_context
!= objfile_context
)
1292 domain_search_flags slot_domain
= slot
->domain
;
1293 if (slot
->state
== SYMBOL_SLOT_NOT_FOUND
)
1294 slot_name
= slot
->value
.name
;
1296 slot_name
= slot
->value
.found
.symbol
->search_name ();
1298 /* NULL names match. */
1299 if (slot_name
== NULL
&& name
== NULL
)
1301 /* But there's no point in calling symbol_matches_domain in the
1302 SYMBOL_SLOT_FOUND case. */
1303 if (slot_domain
!= domain
)
1306 else if (slot_name
!= NULL
&& name
!= NULL
)
1308 /* It's important that we use the same comparison that was done
1309 the first time through. If the slot records a found symbol,
1310 then this means using the symbol name comparison function of
1311 the symbol's language with symbol->search_name (). See
1314 If the slot records a not-found symbol, then require a precise match.
1315 We could still be lax with whitespace like strcmp_iw though. */
1317 if (slot_domain
!= domain
)
1320 if (slot
->state
== SYMBOL_SLOT_NOT_FOUND
)
1322 if (strcmp (slot_name
, name
) != 0)
1327 struct symbol
*sym
= slot
->value
.found
.symbol
;
1328 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
1330 if (!symbol_matches_search_name (sym
, lookup_name
))
1336 /* Only one name is NULL. */
1343 /* Given a cache of size SIZE, return the size of the struct (with variable
1344 length array) in bytes. */
1347 symbol_cache_byte_size (unsigned int size
)
1349 return (sizeof (struct block_symbol_cache
)
1350 + ((size
- 1) * sizeof (struct symbol_cache_slot
)));
1356 resize_symbol_cache (struct symbol_cache
*cache
, unsigned int new_size
)
1358 /* If there's no change in size, don't do anything.
1359 All caches have the same size, so we can just compare with the size
1360 of the global symbols cache. */
1361 if ((cache
->global_symbols
!= NULL
1362 && cache
->global_symbols
->size
== new_size
)
1363 || (cache
->global_symbols
== NULL
1367 destroy_block_symbol_cache (cache
->global_symbols
);
1368 destroy_block_symbol_cache (cache
->static_symbols
);
1372 cache
->global_symbols
= NULL
;
1373 cache
->static_symbols
= NULL
;
1377 size_t total_size
= symbol_cache_byte_size (new_size
);
1379 cache
->global_symbols
1380 = (struct block_symbol_cache
*) xcalloc (1, total_size
);
1381 cache
->static_symbols
1382 = (struct block_symbol_cache
*) xcalloc (1, total_size
);
1383 cache
->global_symbols
->size
= new_size
;
1384 cache
->static_symbols
->size
= new_size
;
1388 /* Return the symbol cache of PSPACE.
1389 Create one if it doesn't exist yet. */
1391 static struct symbol_cache
*
1392 get_symbol_cache (struct program_space
*pspace
)
1394 struct symbol_cache
*cache
= symbol_cache_key
.get (pspace
);
1398 cache
= symbol_cache_key
.emplace (pspace
);
1399 resize_symbol_cache (cache
, symbol_cache_size
);
1405 /* Set the size of the symbol cache in all program spaces. */
1408 set_symbol_cache_size (unsigned int new_size
)
1410 for (struct program_space
*pspace
: program_spaces
)
1412 struct symbol_cache
*cache
= symbol_cache_key
.get (pspace
);
1414 /* The pspace could have been created but not have a cache yet. */
1416 resize_symbol_cache (cache
, new_size
);
1420 /* Called when symbol-cache-size is set. */
1423 set_symbol_cache_size_handler (const char *args
, int from_tty
,
1424 struct cmd_list_element
*c
)
1426 if (new_symbol_cache_size
> MAX_SYMBOL_CACHE_SIZE
)
1428 /* Restore the previous value.
1429 This is the value the "show" command prints. */
1430 new_symbol_cache_size
= symbol_cache_size
;
1432 error (_("Symbol cache size is too large, max is %u."),
1433 MAX_SYMBOL_CACHE_SIZE
);
1435 symbol_cache_size
= new_symbol_cache_size
;
1437 set_symbol_cache_size (symbol_cache_size
);
1440 /* Lookup symbol NAME,DOMAIN in BLOCK in the symbol cache of PSPACE.
1441 OBJFILE_CONTEXT is the current objfile, which may be NULL.
1442 The result is the symbol if found, SYMBOL_LOOKUP_FAILED if a previous lookup
1443 failed (and thus this one will too), or NULL if the symbol is not present
1445 *BSC_PTR and *SLOT_PTR are set to the cache and slot of the symbol, which
1446 can be used to save the result of a full lookup attempt. */
1448 static struct block_symbol
1449 symbol_cache_lookup (struct symbol_cache
*cache
,
1450 struct objfile
*objfile_context
, enum block_enum block
,
1451 const char *name
, domain_search_flags domain
,
1452 struct block_symbol_cache
**bsc_ptr
,
1453 struct symbol_cache_slot
**slot_ptr
)
1455 struct block_symbol_cache
*bsc
;
1457 struct symbol_cache_slot
*slot
;
1459 if (block
== GLOBAL_BLOCK
)
1460 bsc
= cache
->global_symbols
;
1462 bsc
= cache
->static_symbols
;
1470 hash
= hash_symbol_entry (objfile_context
, name
, domain
);
1471 slot
= bsc
->symbols
+ hash
% bsc
->size
;
1476 if (eq_symbol_entry (slot
, objfile_context
, name
, domain
))
1478 symbol_lookup_debug_printf ("%s block symbol cache hit%s for %s, %s",
1479 block
== GLOBAL_BLOCK
? "Global" : "Static",
1480 slot
->state
== SYMBOL_SLOT_NOT_FOUND
1481 ? " (not found)" : "", name
,
1482 domain_name (domain
).c_str ());
1484 if (slot
->state
== SYMBOL_SLOT_NOT_FOUND
)
1485 return SYMBOL_LOOKUP_FAILED
;
1486 return slot
->value
.found
;
1489 /* Symbol is not present in the cache. */
1491 symbol_lookup_debug_printf ("%s block symbol cache miss for %s, %s",
1492 block
== GLOBAL_BLOCK
? "Global" : "Static",
1493 name
, domain_name (domain
).c_str ());
1498 /* Mark SYMBOL as found in SLOT.
1499 OBJFILE_CONTEXT is the current objfile when the lookup was done, or NULL
1500 if it's not needed to distinguish lookups (STATIC_BLOCK). It is *not*
1501 necessarily the objfile the symbol was found in. */
1504 symbol_cache_mark_found (struct block_symbol_cache
*bsc
,
1505 struct symbol_cache_slot
*slot
,
1506 struct objfile
*objfile_context
,
1507 struct symbol
*symbol
,
1508 const struct block
*block
,
1509 domain_search_flags domain
)
1513 if (slot
->state
!= SYMBOL_SLOT_UNUSED
)
1516 symbol_cache_clear_slot (slot
);
1518 slot
->state
= SYMBOL_SLOT_FOUND
;
1519 slot
->objfile_context
= objfile_context
;
1520 slot
->value
.found
.symbol
= symbol
;
1521 slot
->value
.found
.block
= block
;
1522 slot
->domain
= domain
;
1525 /* Mark symbol NAME, DOMAIN as not found in SLOT.
1526 OBJFILE_CONTEXT is the current objfile when the lookup was done, or NULL
1527 if it's not needed to distinguish lookups (STATIC_BLOCK). */
1530 symbol_cache_mark_not_found (struct block_symbol_cache
*bsc
,
1531 struct symbol_cache_slot
*slot
,
1532 struct objfile
*objfile_context
,
1533 const char *name
, domain_search_flags domain
)
1537 if (slot
->state
!= SYMBOL_SLOT_UNUSED
)
1540 symbol_cache_clear_slot (slot
);
1542 slot
->state
= SYMBOL_SLOT_NOT_FOUND
;
1543 slot
->objfile_context
= objfile_context
;
1544 slot
->value
.name
= xstrdup (name
);
1545 slot
->domain
= domain
;
1548 /* Flush the symbol cache of PSPACE. */
1551 symbol_cache_flush (struct program_space
*pspace
)
1553 ada_clear_symbol_cache (pspace
);
1554 struct symbol_cache
*cache
= symbol_cache_key
.get (pspace
);
1559 if (cache
->global_symbols
== NULL
)
1561 gdb_assert (symbol_cache_size
== 0);
1562 gdb_assert (cache
->static_symbols
== NULL
);
1566 /* If the cache is untouched since the last flush, early exit.
1567 This is important for performance during the startup of a program linked
1568 with 100s (or 1000s) of shared libraries. */
1569 if (cache
->global_symbols
->misses
== 0
1570 && cache
->static_symbols
->misses
== 0)
1573 gdb_assert (cache
->global_symbols
->size
== symbol_cache_size
);
1574 gdb_assert (cache
->static_symbols
->size
== symbol_cache_size
);
1576 for (pass
= 0; pass
< 2; ++pass
)
1578 struct block_symbol_cache
*bsc
1579 = pass
== 0 ? cache
->global_symbols
: cache
->static_symbols
;
1582 for (i
= 0; i
< bsc
->size
; ++i
)
1583 symbol_cache_clear_slot (&bsc
->symbols
[i
]);
1586 cache
->global_symbols
->hits
= 0;
1587 cache
->global_symbols
->misses
= 0;
1588 cache
->global_symbols
->collisions
= 0;
1589 cache
->static_symbols
->hits
= 0;
1590 cache
->static_symbols
->misses
= 0;
1591 cache
->static_symbols
->collisions
= 0;
1597 symbol_cache_dump (const struct symbol_cache
*cache
)
1601 if (cache
->global_symbols
== NULL
)
1603 gdb_printf (" <disabled>\n");
1607 for (pass
= 0; pass
< 2; ++pass
)
1609 const struct block_symbol_cache
*bsc
1610 = pass
== 0 ? cache
->global_symbols
: cache
->static_symbols
;
1614 gdb_printf ("Global symbols:\n");
1616 gdb_printf ("Static symbols:\n");
1618 for (i
= 0; i
< bsc
->size
; ++i
)
1620 const struct symbol_cache_slot
*slot
= &bsc
->symbols
[i
];
1624 switch (slot
->state
)
1626 case SYMBOL_SLOT_UNUSED
:
1628 case SYMBOL_SLOT_NOT_FOUND
:
1629 gdb_printf (" [%4u] = %s, %s %s (not found)\n", i
,
1630 host_address_to_string (slot
->objfile_context
),
1632 domain_name (slot
->domain
).c_str ());
1634 case SYMBOL_SLOT_FOUND
:
1636 struct symbol
*found
= slot
->value
.found
.symbol
;
1637 const struct objfile
*context
= slot
->objfile_context
;
1639 gdb_printf (" [%4u] = %s, %s %s\n", i
,
1640 host_address_to_string (context
),
1641 found
->print_name (),
1642 domain_name (found
->domain ()));
1650 /* The "mt print symbol-cache" command. */
1653 maintenance_print_symbol_cache (const char *args
, int from_tty
)
1655 for (struct program_space
*pspace
: program_spaces
)
1657 struct symbol_cache
*cache
;
1659 gdb_printf (_("Symbol cache for pspace %d\n%s:\n"),
1661 pspace
->symfile_object_file
!= NULL
1662 ? objfile_name (pspace
->symfile_object_file
)
1663 : "(no object file)");
1665 /* If the cache hasn't been created yet, avoid creating one. */
1666 cache
= symbol_cache_key
.get (pspace
);
1668 gdb_printf (" <empty>\n");
1670 symbol_cache_dump (cache
);
1674 /* The "mt flush-symbol-cache" command. */
1677 maintenance_flush_symbol_cache (const char *args
, int from_tty
)
1679 for (struct program_space
*pspace
: program_spaces
)
1681 symbol_cache_flush (pspace
);
1685 /* Print usage statistics of CACHE. */
1688 symbol_cache_stats (struct symbol_cache
*cache
)
1692 if (cache
->global_symbols
== NULL
)
1694 gdb_printf (" <disabled>\n");
1698 for (pass
= 0; pass
< 2; ++pass
)
1700 const struct block_symbol_cache
*bsc
1701 = pass
== 0 ? cache
->global_symbols
: cache
->static_symbols
;
1706 gdb_printf ("Global block cache stats:\n");
1708 gdb_printf ("Static block cache stats:\n");
1710 gdb_printf (" size: %u\n", bsc
->size
);
1711 gdb_printf (" hits: %u\n", bsc
->hits
);
1712 gdb_printf (" misses: %u\n", bsc
->misses
);
1713 gdb_printf (" collisions: %u\n", bsc
->collisions
);
1717 /* The "mt print symbol-cache-statistics" command. */
1720 maintenance_print_symbol_cache_statistics (const char *args
, int from_tty
)
1722 for (struct program_space
*pspace
: program_spaces
)
1724 struct symbol_cache
*cache
;
1726 gdb_printf (_("Symbol cache statistics for pspace %d\n%s:\n"),
1728 pspace
->symfile_object_file
!= NULL
1729 ? objfile_name (pspace
->symfile_object_file
)
1730 : "(no object file)");
1732 /* If the cache hasn't been created yet, avoid creating one. */
1733 cache
= symbol_cache_key
.get (pspace
);
1735 gdb_printf (" empty, no stats available\n");
1737 symbol_cache_stats (cache
);
1741 /* This module's 'new_objfile' observer. */
1744 symtab_new_objfile_observer (struct objfile
*objfile
)
1746 symbol_cache_flush (objfile
->pspace ());
1749 /* This module's 'all_objfiles_removed' observer. */
1752 symtab_all_objfiles_removed (program_space
*pspace
)
1754 symbol_cache_flush (pspace
);
1756 /* Forget everything we know about the main function. */
1757 main_progspace_key
.clear (pspace
);
1760 /* This module's 'free_objfile' observer. */
1763 symtab_free_objfile_observer (struct objfile
*objfile
)
1765 symbol_cache_flush (objfile
->pspace ());
1771 fixup_symbol_section (struct symbol
*sym
, struct objfile
*objfile
)
1773 gdb_assert (sym
!= nullptr);
1774 gdb_assert (sym
->is_objfile_owned ());
1775 gdb_assert (objfile
!= nullptr);
1776 gdb_assert (sym
->section_index () == -1);
1778 /* Note that if this ends up as -1, fixup_section will handle that
1779 reasonably well. So, it's fine to use the objfile's section
1780 index without doing the check that is done by the wrapper macros
1781 like SECT_OFF_TEXT. */
1783 switch (sym
->aclass ())
1786 fallback
= objfile
->sect_index_data
;
1790 fallback
= objfile
->sect_index_text
;
1794 /* Nothing else will be listed in the minsyms -- no use looking
1799 CORE_ADDR addr
= sym
->value_address ();
1801 struct minimal_symbol
*msym
;
1803 /* First, check whether a minimal symbol with the same name exists
1804 and points to the same address. The address check is required
1805 e.g. on PowerPC64, where the minimal symbol for a function will
1806 point to the function descriptor, while the debug symbol will
1807 point to the actual function code. */
1808 msym
= lookup_minimal_symbol_by_pc_name (addr
, sym
->linkage_name (),
1811 sym
->set_section_index (msym
->section_index ());
1814 /* Static, function-local variables do appear in the linker
1815 (minimal) symbols, but are frequently given names that won't
1816 be found via lookup_minimal_symbol(). E.g., it has been
1817 observed in frv-uclinux (ELF) executables that a static,
1818 function-local variable named "foo" might appear in the
1819 linker symbols as "foo.6" or "foo.3". Thus, there is no
1820 point in attempting to extend the lookup-by-name mechanism to
1821 handle this case due to the fact that there can be multiple
1824 So, instead, search the section table when lookup by name has
1825 failed. The ``addr'' and ``endaddr'' fields may have already
1826 been relocated. If so, the relocation offset needs to be
1827 subtracted from these values when performing the comparison.
1828 We unconditionally subtract it, because, when no relocation
1829 has been performed, the value will simply be zero.
1831 The address of the symbol whose section we're fixing up HAS
1832 NOT BEEN adjusted (relocated) yet. It can't have been since
1833 the section isn't yet known and knowing the section is
1834 necessary in order to add the correct relocation value. In
1835 other words, we wouldn't even be in this function (attempting
1836 to compute the section) if it were already known.
1838 Note that it is possible to search the minimal symbols
1839 (subtracting the relocation value if necessary) to find the
1840 matching minimal symbol, but this is overkill and much less
1841 efficient. It is not necessary to find the matching minimal
1842 symbol, only its section.
1844 Note that this technique (of doing a section table search)
1845 can fail when unrelocated section addresses overlap. For
1846 this reason, we still attempt a lookup by name prior to doing
1847 a search of the section table. */
1849 for (obj_section
*s
: objfile
->sections ())
1851 if ((bfd_section_flags (s
->the_bfd_section
) & SEC_ALLOC
) == 0)
1854 int idx
= s
- objfile
->sections_start
;
1855 CORE_ADDR offset
= objfile
->section_offsets
[idx
];
1860 if (s
->addr () - offset
<= addr
&& addr
< s
->endaddr () - offset
)
1862 sym
->set_section_index (idx
);
1867 /* If we didn't find the section, assume it is in the first
1868 section. If there is no allocated section, then it hardly
1869 matters what we pick, so just pick zero. */
1871 sym
->set_section_index (0);
1873 sym
->set_section_index (fallback
);
1879 demangle_for_lookup_info::demangle_for_lookup_info
1880 (const lookup_name_info
&lookup_name
, language lang
)
1882 demangle_result_storage storage
;
1884 if (lookup_name
.ignore_parameters () && lang
== language_cplus
)
1886 gdb::unique_xmalloc_ptr
<char> without_params
1887 = cp_remove_params_if_any (lookup_name
.c_str (),
1888 lookup_name
.completion_mode ());
1890 if (without_params
!= NULL
)
1892 if (lookup_name
.match_type () != symbol_name_match_type::SEARCH_NAME
)
1893 m_demangled_name
= demangle_for_lookup (without_params
.get (),
1899 if (lookup_name
.match_type () == symbol_name_match_type::SEARCH_NAME
)
1900 m_demangled_name
= lookup_name
.c_str ();
1902 m_demangled_name
= demangle_for_lookup (lookup_name
.c_str (),
1908 const lookup_name_info
&
1909 lookup_name_info::match_any ()
1911 /* Lookup any symbol that "" would complete. I.e., this matches all
1913 static const lookup_name_info
lookup_name ("", symbol_name_match_type::WILD
,
1922 lookup_name_info::search_name_hash (language lang
) const
1924 /* This works around an obscure problem. If currently in Ada mode,
1925 and the name is wrapped in '<...>' (indicating verbatim mode),
1926 force the use of the Ada language here so that the '<' and '>'
1928 if (current_language
->la_language
== language_ada
&& ada ().verbatim_p ())
1929 lang
= language_ada
;
1931 /* Only compute each language's hash once. */
1932 if (!m_demangled_hashes_p
[lang
])
1934 m_demangled_hashes
[lang
]
1935 = ::search_name_hash (lang
, language_lookup_name (lang
));
1936 m_demangled_hashes_p
[lang
] = true;
1938 return m_demangled_hashes
[lang
];
1941 /* Compute the demangled form of NAME as used by the various symbol
1942 lookup functions. The result can either be the input NAME
1943 directly, or a pointer to a buffer owned by the STORAGE object.
1945 For Ada, this function just returns NAME, unmodified.
1946 Normally, Ada symbol lookups are performed using the encoded name
1947 rather than the demangled name, and so it might seem to make sense
1948 for this function to return an encoded version of NAME.
1949 Unfortunately, we cannot do this, because this function is used in
1950 circumstances where it is not appropriate to try to encode NAME.
1951 For instance, when displaying the frame info, we demangle the name
1952 of each parameter, and then perform a symbol lookup inside our
1953 function using that demangled name. In Ada, certain functions
1954 have internally-generated parameters whose name contain uppercase
1955 characters. Encoding those name would result in those uppercase
1956 characters to become lowercase, and thus cause the symbol lookup
1960 demangle_for_lookup (const char *name
, enum language lang
,
1961 demangle_result_storage
&storage
)
1963 /* If we are using C++, D, or Go, demangle the name before doing a
1964 lookup, so we can always binary search. */
1965 if (lang
== language_cplus
)
1967 gdb::unique_xmalloc_ptr
<char> demangled_name
1968 = gdb_demangle (name
, DMGL_ANSI
| DMGL_PARAMS
);
1969 if (demangled_name
!= NULL
)
1970 return storage
.set_malloc_ptr (std::move (demangled_name
));
1972 /* If we were given a non-mangled name, canonicalize it
1973 according to the language (so far only for C++). */
1974 gdb::unique_xmalloc_ptr
<char> canon
= cp_canonicalize_string (name
);
1975 if (canon
!= nullptr)
1976 return storage
.set_malloc_ptr (std::move (canon
));
1978 else if (lang
== language_d
)
1980 gdb::unique_xmalloc_ptr
<char> demangled_name
= d_demangle (name
, 0);
1981 if (demangled_name
!= NULL
)
1982 return storage
.set_malloc_ptr (std::move (demangled_name
));
1984 else if (lang
== language_go
)
1986 gdb::unique_xmalloc_ptr
<char> demangled_name
1987 = language_def (language_go
)->demangle_symbol (name
, 0);
1988 if (demangled_name
!= NULL
)
1989 return storage
.set_malloc_ptr (std::move (demangled_name
));
1998 search_name_hash (enum language language
, const char *search_name
)
2000 return language_def (language
)->search_name_hash (search_name
);
2005 This function (or rather its subordinates) have a bunch of loops and
2006 it would seem to be attractive to put in some QUIT's (though I'm not really
2007 sure whether it can run long enough to be really important). But there
2008 are a few calls for which it would appear to be bad news to quit
2009 out of here: e.g., find_proc_desc in alpha-mdebug-tdep.c. (Note
2010 that there is C++ code below which can error(), but that probably
2011 doesn't affect these calls since they are looking for a known
2012 variable and thus can probably assume it will never hit the C++
2016 lookup_symbol_in_language (const char *name
, const struct block
*block
,
2017 const domain_search_flags domain
,
2019 struct field_of_this_result
*is_a_field_of_this
)
2021 SYMBOL_LOOKUP_SCOPED_DEBUG_ENTER_EXIT
;
2023 demangle_result_storage storage
;
2024 const char *modified_name
= demangle_for_lookup (name
, lang
, storage
);
2026 return lookup_symbol_aux (modified_name
,
2027 symbol_name_match_type::FULL
,
2028 block
, domain
, lang
,
2029 is_a_field_of_this
);
2035 lookup_symbol (const char *name
, const struct block
*block
,
2036 domain_search_flags domain
,
2037 struct field_of_this_result
*is_a_field_of_this
)
2039 return lookup_symbol_in_language (name
, block
, domain
,
2040 current_language
->la_language
,
2041 is_a_field_of_this
);
2047 lookup_symbol_search_name (const char *search_name
, const struct block
*block
,
2048 domain_search_flags domain
)
2050 return lookup_symbol_aux (search_name
, symbol_name_match_type::SEARCH_NAME
,
2051 block
, domain
, language_asm
, NULL
);
2057 lookup_language_this (const struct language_defn
*lang
,
2058 const struct block
*block
)
2060 if (lang
->name_of_this () == NULL
|| block
== NULL
)
2063 symbol_lookup_debug_printf_v ("lookup_language_this (%s, %s (objfile %s))",
2064 lang
->name (), host_address_to_string (block
),
2065 objfile_debug_name (block
->objfile ()));
2067 lookup_name_info
this_name (lang
->name_of_this (),
2068 symbol_name_match_type::SEARCH_NAME
);
2074 sym
= block_lookup_symbol (block
, this_name
, SEARCH_VFT
);
2077 symbol_lookup_debug_printf_v
2078 ("lookup_language_this (...) = %s (%s, block %s)",
2079 sym
->print_name (), host_address_to_string (sym
),
2080 host_address_to_string (block
));
2081 return (struct block_symbol
) {sym
, block
};
2083 if (block
->function ())
2085 block
= block
->superblock ();
2088 symbol_lookup_debug_printf_v ("lookup_language_this (...) = NULL");
2092 /* Given TYPE, a structure/union,
2093 return 1 if the component named NAME from the ultimate target
2094 structure/union is defined, otherwise, return 0. */
2097 check_field (struct type
*type
, const char *name
,
2098 struct field_of_this_result
*is_a_field_of_this
)
2102 /* The type may be a stub. */
2103 type
= check_typedef (type
);
2105 for (i
= type
->num_fields () - 1; i
>= TYPE_N_BASECLASSES (type
); i
--)
2107 const char *t_field_name
= type
->field (i
).name ();
2109 if (t_field_name
&& (strcmp_iw (t_field_name
, name
) == 0))
2111 is_a_field_of_this
->type
= type
;
2112 is_a_field_of_this
->field
= &type
->field (i
);
2117 /* C++: If it was not found as a data field, then try to return it
2118 as a pointer to a method. */
2120 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; --i
)
2122 if (strcmp_iw (TYPE_FN_FIELDLIST_NAME (type
, i
), name
) == 0)
2124 is_a_field_of_this
->type
= type
;
2125 is_a_field_of_this
->fn_field
= &TYPE_FN_FIELDLIST (type
, i
);
2130 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
2131 if (check_field (TYPE_BASECLASS (type
, i
), name
, is_a_field_of_this
))
2137 /* Behave like lookup_symbol except that NAME is the natural name
2138 (e.g., demangled name) of the symbol that we're looking for. */
2140 static struct block_symbol
2141 lookup_symbol_aux (const char *name
, symbol_name_match_type match_type
,
2142 const struct block
*block
,
2143 const domain_search_flags domain
, enum language language
,
2144 struct field_of_this_result
*is_a_field_of_this
)
2146 SYMBOL_LOOKUP_SCOPED_DEBUG_ENTER_EXIT
;
2148 struct block_symbol result
;
2149 const struct language_defn
*langdef
;
2151 if (symbol_lookup_debug
)
2153 struct objfile
*objfile
= (block
== nullptr
2154 ? nullptr : block
->objfile ());
2156 symbol_lookup_debug_printf
2157 ("demangled symbol name = \"%s\", block @ %s (objfile %s)",
2158 name
, host_address_to_string (block
),
2159 objfile
!= NULL
? objfile_debug_name (objfile
) : "NULL");
2160 symbol_lookup_debug_printf
2161 ("domain name = \"%s\", language = \"%s\")",
2162 domain_name (domain
).c_str (), language_str (language
));
2165 langdef
= language_def (language
);
2167 /* Search specified block and its superiors. Don't search
2168 STATIC_BLOCK or GLOBAL_BLOCK. */
2170 result
= lookup_local_symbol (name
, match_type
, block
, domain
, langdef
);
2171 if (result
.symbol
!= NULL
)
2173 symbol_lookup_debug_printf
2174 ("found symbol @ %s (using lookup_local_symbol)",
2175 host_address_to_string (result
.symbol
));
2179 /* If requested to do so by the caller and if appropriate for LANGUAGE,
2180 check to see if NAME is a field of `this'. */
2182 /* Don't do this check if we are searching for a struct. It will
2183 not be found by check_field, but will be found by other
2185 if (is_a_field_of_this
!= NULL
&& (domain
& SEARCH_STRUCT_DOMAIN
) == 0)
2187 result
= lookup_language_this (langdef
, block
);
2191 struct type
*t
= result
.symbol
->type ();
2193 /* I'm not really sure that type of this can ever
2194 be typedefed; just be safe. */
2195 t
= check_typedef (t
);
2196 if (t
->is_pointer_or_reference ())
2197 t
= t
->target_type ();
2199 if (t
->code () != TYPE_CODE_STRUCT
2200 && t
->code () != TYPE_CODE_UNION
)
2201 error (_("Internal error: `%s' is not an aggregate"),
2202 langdef
->name_of_this ());
2204 if (check_field (t
, name
, is_a_field_of_this
))
2206 symbol_lookup_debug_printf ("no symbol found");
2212 /* Now do whatever is appropriate for LANGUAGE to look
2213 up static and global variables. */
2215 result
= langdef
->lookup_symbol_nonlocal (name
, block
, domain
);
2216 if (result
.symbol
!= NULL
)
2218 symbol_lookup_debug_printf
2219 ("found symbol @ %s (using language lookup_symbol_nonlocal)",
2220 host_address_to_string (result
.symbol
));
2224 /* Now search all static file-level symbols. Not strictly correct,
2225 but more useful than an error. */
2227 result
= lookup_static_symbol (name
, domain
);
2228 symbol_lookup_debug_printf
2229 ("found symbol @ %s (using lookup_static_symbol)",
2230 result
.symbol
!= NULL
? host_address_to_string (result
.symbol
) : "NULL");
2234 /* Check to see if the symbol is defined in BLOCK or its superiors.
2235 Don't search STATIC_BLOCK or GLOBAL_BLOCK. */
2237 static struct block_symbol
2238 lookup_local_symbol (const char *name
,
2239 symbol_name_match_type match_type
,
2240 const struct block
*block
,
2241 const domain_search_flags domain
,
2242 const struct language_defn
*langdef
)
2244 if (block
== nullptr)
2247 const char *scope
= block
->scope ();
2249 while (!block
->is_global_block () && !block
->is_static_block ())
2251 struct symbol
*sym
= lookup_symbol_in_block (name
, match_type
,
2254 return (struct block_symbol
) {sym
, block
};
2256 struct symbol
*function
= block
->function ();
2257 if (function
!= nullptr && function
->is_template_function ())
2259 struct template_symbol
*templ
= (struct template_symbol
*) function
;
2260 sym
= search_symbol_list (name
,
2261 templ
->n_template_arguments
,
2262 templ
->template_arguments
);
2264 return (struct block_symbol
) {sym
, block
};
2267 struct block_symbol blocksym
2268 = langdef
->lookup_symbol_local (scope
, name
, block
, domain
);
2269 if (blocksym
.symbol
!= nullptr)
2272 if (block
->inlined_p ())
2274 block
= block
->superblock ();
2277 /* We've reached the end of the function without finding a result. */
2285 lookup_symbol_in_block (const char *name
, symbol_name_match_type match_type
,
2286 const struct block
*block
,
2287 const domain_search_flags domain
)
2289 enter_symbol_lookup tmp
;
2293 if (symbol_lookup_debug
)
2295 struct objfile
*objfile
2296 = block
== nullptr ? nullptr : block
->objfile ();
2298 symbol_lookup_debug_printf_v
2299 ("lookup_symbol_in_block (%s, %s (objfile %s), %s)",
2300 name
, host_address_to_string (block
),
2301 objfile
!= nullptr ? objfile_debug_name (objfile
) : "NULL",
2302 domain_name (domain
).c_str ());
2305 lookup_name_info
lookup_name (name
, match_type
);
2306 sym
= block_lookup_symbol (block
, lookup_name
, domain
);
2309 symbol_lookup_debug_printf_v ("lookup_symbol_in_block (...) = %s",
2310 host_address_to_string (sym
));
2314 symbol_lookup_debug_printf_v ("lookup_symbol_in_block (...) = NULL");
2321 lookup_global_symbol_from_objfile (struct objfile
*main_objfile
,
2322 enum block_enum block_index
,
2324 const domain_search_flags domain
)
2326 enter_symbol_lookup tmp
;
2328 gdb_assert (block_index
== GLOBAL_BLOCK
|| block_index
== STATIC_BLOCK
);
2330 for (objfile
*objfile
: main_objfile
->separate_debug_objfiles ())
2332 struct block_symbol result
2333 = lookup_symbol_in_objfile (objfile
, block_index
, name
, domain
);
2335 if (result
.symbol
!= nullptr)
2342 /* Check to see if the symbol is defined in one of the OBJFILE's
2343 symtabs. BLOCK_INDEX should be either GLOBAL_BLOCK or STATIC_BLOCK,
2344 depending on whether or not we want to search global symbols or
2347 static struct block_symbol
2348 lookup_symbol_in_objfile_symtabs (struct objfile
*objfile
,
2349 enum block_enum block_index
, const char *name
,
2350 const domain_search_flags domain
)
2352 gdb_assert (block_index
== GLOBAL_BLOCK
|| block_index
== STATIC_BLOCK
);
2354 symbol_lookup_debug_printf_v
2355 ("lookup_symbol_in_objfile_symtabs (%s, %s, %s, %s)",
2356 objfile_debug_name (objfile
),
2357 block_index
== GLOBAL_BLOCK
? "GLOBAL_BLOCK" : "STATIC_BLOCK",
2358 name
, domain_name (domain
).c_str ());
2360 struct block_symbol other
;
2361 other
.symbol
= NULL
;
2362 for (compunit_symtab
*cust
: objfile
->compunits ())
2364 const struct blockvector
*bv
;
2365 const struct block
*block
;
2366 struct block_symbol result
;
2368 bv
= cust
->blockvector ();
2369 block
= bv
->block (block_index
);
2370 result
.symbol
= block_lookup_symbol_primary (block
, name
, domain
);
2371 result
.block
= block
;
2372 if (result
.symbol
== NULL
)
2374 if (best_symbol (result
.symbol
, domain
))
2379 if (result
.symbol
->matches (domain
))
2381 struct symbol
*better
2382 = better_symbol (other
.symbol
, result
.symbol
, domain
);
2383 if (better
!= other
.symbol
)
2385 other
.symbol
= better
;
2386 other
.block
= block
;
2391 if (other
.symbol
!= NULL
)
2393 symbol_lookup_debug_printf_v
2394 ("lookup_symbol_in_objfile_symtabs (...) = %s (block %s)",
2395 host_address_to_string (other
.symbol
),
2396 host_address_to_string (other
.block
));
2400 symbol_lookup_debug_printf_v
2401 ("lookup_symbol_in_objfile_symtabs (...) = NULL");
2405 /* Wrapper around lookup_symbol_in_objfile_symtabs for search_symbols.
2406 Look up LINKAGE_NAME in DOMAIN in the global and static blocks of OBJFILE
2407 and all associated separate debug objfiles.
2409 Normally we only look in OBJFILE, and not any separate debug objfiles
2410 because the outer loop will cause them to be searched too. This case is
2411 different. Here we're called from search_symbols where it will only
2412 call us for the objfile that contains a matching minsym. */
2414 static struct block_symbol
2415 lookup_symbol_in_objfile_from_linkage_name (struct objfile
*objfile
,
2416 const char *linkage_name
,
2417 domain_search_flags domain
)
2419 enum language lang
= current_language
->la_language
;
2420 struct objfile
*main_objfile
;
2422 demangle_result_storage storage
;
2423 const char *modified_name
= demangle_for_lookup (linkage_name
, lang
, storage
);
2425 if (objfile
->separate_debug_objfile_backlink
)
2426 main_objfile
= objfile
->separate_debug_objfile_backlink
;
2428 main_objfile
= objfile
;
2430 for (::objfile
*cur_objfile
: main_objfile
->separate_debug_objfiles ())
2432 struct block_symbol result
;
2434 result
= lookup_symbol_in_objfile_symtabs (cur_objfile
, GLOBAL_BLOCK
,
2435 modified_name
, domain
);
2436 if (result
.symbol
== NULL
)
2437 result
= lookup_symbol_in_objfile_symtabs (cur_objfile
, STATIC_BLOCK
,
2438 modified_name
, domain
);
2439 if (result
.symbol
!= NULL
)
2446 /* A helper function that throws an exception when a symbol was found
2447 in a psymtab but not in a symtab. */
2449 [[noreturn
]] static void
2450 error_in_psymtab_expansion (enum block_enum block_index
, const char *name
,
2451 struct compunit_symtab
*cust
)
2454 Internal: %s symbol `%s' found in %s psymtab but not in symtab.\n\
2455 %s may be an inlined function, or may be a template function\n \
2456 (if a template, try specifying an instantiation: %s<type>)."),
2457 block_index
== GLOBAL_BLOCK
? "global" : "static",
2459 symtab_to_filename_for_display (cust
->primary_filetab ()),
2463 /* A helper function for various lookup routines that interfaces with
2464 the "quick" symbol table functions. */
2466 static struct block_symbol
2467 lookup_symbol_via_quick_fns (struct objfile
*objfile
,
2468 enum block_enum block_index
, const char *name
,
2469 const domain_search_flags domain
)
2471 struct compunit_symtab
*cust
;
2472 const struct blockvector
*bv
;
2473 const struct block
*block
;
2474 struct block_symbol result
;
2476 symbol_lookup_debug_printf_v
2477 ("lookup_symbol_via_quick_fns (%s, %s, %s, %s)",
2478 objfile_debug_name (objfile
),
2479 block_index
== GLOBAL_BLOCK
? "GLOBAL_BLOCK" : "STATIC_BLOCK",
2480 name
, domain_name (domain
).c_str ());
2482 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
2483 cust
= objfile
->lookup_symbol (block_index
, lookup_name
, domain
);
2486 symbol_lookup_debug_printf_v
2487 ("lookup_symbol_via_quick_fns (...) = NULL");
2491 bv
= cust
->blockvector ();
2492 block
= bv
->block (block_index
);
2493 result
.symbol
= block_lookup_symbol (block
, lookup_name
, domain
);
2494 if (result
.symbol
== NULL
)
2495 error_in_psymtab_expansion (block_index
, name
, cust
);
2497 symbol_lookup_debug_printf_v
2498 ("lookup_symbol_via_quick_fns (...) = %s (block %s)",
2499 host_address_to_string (result
.symbol
),
2500 host_address_to_string (block
));
2502 result
.block
= block
;
2506 /* See language.h. */
2509 language_defn::lookup_symbol_nonlocal (const char *name
,
2510 const struct block
*block
,
2511 const domain_search_flags domain
) const
2513 struct block_symbol result
;
2515 /* NOTE: dje/2014-10-26: The lookup in all objfiles search could skip
2516 the current objfile. Searching the current objfile first is useful
2517 for both matching user expectations as well as performance. */
2519 result
= lookup_symbol_in_static_block (name
, block
, domain
);
2520 if (result
.symbol
!= NULL
)
2523 /* If we didn't find a definition for a builtin type in the static block,
2524 search for it now. This is actually the right thing to do and can be
2525 a massive performance win. E.g., when debugging a program with lots of
2526 shared libraries we could search all of them only to find out the
2527 builtin type isn't defined in any of them. This is common for types
2529 if ((domain
& SEARCH_TYPE_DOMAIN
) != 0)
2531 struct gdbarch
*gdbarch
;
2534 gdbarch
= current_inferior ()->arch ();
2536 gdbarch
= block
->gdbarch ();
2537 result
.symbol
= language_lookup_primitive_type_as_symbol (this,
2539 result
.block
= NULL
;
2540 if (result
.symbol
!= NULL
)
2544 return lookup_global_symbol (name
, block
, domain
);
2550 lookup_symbol_in_static_block (const char *name
,
2551 const struct block
*block
,
2552 const domain_search_flags domain
)
2554 if (block
== nullptr)
2557 const struct block
*static_block
= block
->static_block ();
2560 if (static_block
== NULL
)
2563 if (symbol_lookup_debug
)
2565 struct objfile
*objfile
= (block
== nullptr
2566 ? nullptr : block
->objfile ());
2568 symbol_lookup_debug_printf
2569 ("lookup_symbol_in_static_block (%s, %s (objfile %s), %s)",
2570 name
, host_address_to_string (block
),
2571 objfile
!= nullptr ? objfile_debug_name (objfile
) : "NULL",
2572 domain_name (domain
).c_str ());
2575 sym
= lookup_symbol_in_block (name
,
2576 symbol_name_match_type::FULL
,
2577 static_block
, domain
);
2578 symbol_lookup_debug_printf ("lookup_symbol_in_static_block (...) = %s",
2580 ? host_address_to_string (sym
) : "NULL");
2581 return (struct block_symbol
) {sym
, static_block
};
2584 /* Perform the standard symbol lookup of NAME in OBJFILE:
2585 1) First search expanded symtabs, and if not found
2586 2) Search the "quick" symtabs (partial or .gdb_index).
2587 BLOCK_INDEX is one of GLOBAL_BLOCK or STATIC_BLOCK. */
2589 static struct block_symbol
2590 lookup_symbol_in_objfile (struct objfile
*objfile
, enum block_enum block_index
,
2591 const char *name
, const domain_search_flags domain
)
2593 struct block_symbol result
;
2595 gdb_assert (block_index
== GLOBAL_BLOCK
|| block_index
== STATIC_BLOCK
);
2597 symbol_lookup_debug_printf ("lookup_symbol_in_objfile (%s, %s, %s, %s)",
2598 objfile_debug_name (objfile
),
2599 block_index
== GLOBAL_BLOCK
2600 ? "GLOBAL_BLOCK" : "STATIC_BLOCK",
2601 name
, domain_name (domain
).c_str ());
2603 result
= lookup_symbol_in_objfile_symtabs (objfile
, block_index
,
2605 if (result
.symbol
!= NULL
)
2607 symbol_lookup_debug_printf
2608 ("lookup_symbol_in_objfile (...) = %s (in symtabs)",
2609 host_address_to_string (result
.symbol
));
2613 result
= lookup_symbol_via_quick_fns (objfile
, block_index
,
2615 symbol_lookup_debug_printf ("lookup_symbol_in_objfile (...) = %s%s",
2616 result
.symbol
!= NULL
2617 ? host_address_to_string (result
.symbol
)
2619 result
.symbol
!= NULL
? " (via quick fns)"
2624 /* This function contains the common code of lookup_{global,static}_symbol.
2625 OBJFILE is only used if BLOCK_INDEX is GLOBAL_SCOPE, in which case it is
2626 the objfile to start the lookup in. */
2628 static struct block_symbol
2629 lookup_global_or_static_symbol (const char *name
,
2630 enum block_enum block_index
,
2631 struct objfile
*objfile
,
2632 const domain_search_flags domain
)
2634 struct symbol_cache
*cache
= get_symbol_cache (current_program_space
);
2635 struct block_symbol result
;
2636 struct block_symbol_cache
*bsc
;
2637 struct symbol_cache_slot
*slot
;
2639 gdb_assert (block_index
== GLOBAL_BLOCK
|| block_index
== STATIC_BLOCK
);
2640 gdb_assert (objfile
== nullptr || block_index
== GLOBAL_BLOCK
);
2642 /* First see if we can find the symbol in the cache.
2643 This works because we use the current objfile to qualify the lookup. */
2644 result
= symbol_cache_lookup (cache
, objfile
, block_index
, name
, domain
,
2646 if (result
.symbol
!= NULL
)
2648 if (SYMBOL_LOOKUP_FAILED_P (result
))
2653 enter_symbol_lookup tmp
;
2655 /* Do a global search (of global blocks, heh). */
2656 if (result
.symbol
== NULL
)
2657 gdbarch_iterate_over_objfiles_in_search_order
2658 (objfile
!= NULL
? objfile
->arch () : current_inferior ()->arch (),
2659 [&result
, block_index
, name
, domain
] (struct objfile
*objfile_iter
)
2661 result
= lookup_symbol_in_objfile (objfile_iter
, block_index
,
2663 return result
.symbol
!= nullptr;
2667 if (result
.symbol
!= NULL
)
2668 symbol_cache_mark_found (bsc
, slot
, objfile
, result
.symbol
, result
.block
,
2671 symbol_cache_mark_not_found (bsc
, slot
, objfile
, name
, domain
);
2679 lookup_static_symbol (const char *name
, const domain_search_flags domain
)
2681 return lookup_global_or_static_symbol (name
, STATIC_BLOCK
, nullptr, domain
);
2687 lookup_global_symbol (const char *name
,
2688 const struct block
*block
,
2689 const domain_search_flags domain
)
2691 /* If a block was passed in, we want to search the corresponding
2692 global block first. This yields "more expected" behavior, and is
2693 needed to support 'FILENAME'::VARIABLE lookups. */
2694 const struct block
*global_block
2695 = block
== nullptr ? nullptr : block
->global_block ();
2697 if (global_block
!= nullptr)
2699 sym
= lookup_symbol_in_block (name
,
2700 symbol_name_match_type::FULL
,
2701 global_block
, domain
);
2702 if (sym
!= NULL
&& best_symbol (sym
, domain
))
2703 return { sym
, global_block
};
2706 struct objfile
*objfile
= nullptr;
2707 if (block
!= nullptr)
2709 objfile
= block
->objfile ();
2710 if (objfile
->separate_debug_objfile_backlink
!= nullptr)
2711 objfile
= objfile
->separate_debug_objfile_backlink
;
2715 = lookup_global_or_static_symbol (name
, GLOBAL_BLOCK
, objfile
, domain
);
2716 if (better_symbol (sym
, bs
.symbol
, domain
) == sym
)
2717 return { sym
, global_block
};
2725 symbol::matches (domain_search_flags flags
) const
2727 /* C++ has a typedef for every tag, and the types are in the struct
2729 if (language () == language_cplus
&& (flags
& SEARCH_TYPE_DOMAIN
) != 0)
2730 flags
|= SEARCH_STRUCT_DOMAIN
;
2732 return search_flags_matches (flags
, m_domain
);
2738 lookup_transparent_type (const char *name
, domain_search_flags flags
)
2740 return current_language
->lookup_transparent_type (name
, flags
);
2743 /* A helper for basic_lookup_transparent_type that interfaces with the
2744 "quick" symbol table functions. */
2746 static struct type
*
2747 basic_lookup_transparent_type_quick (struct objfile
*objfile
,
2748 enum block_enum block_index
,
2749 domain_search_flags flags
,
2750 const lookup_name_info
&name
)
2752 struct compunit_symtab
*cust
;
2753 const struct blockvector
*bv
;
2754 const struct block
*block
;
2757 cust
= objfile
->lookup_symbol (block_index
, name
, flags
);
2761 bv
= cust
->blockvector ();
2762 block
= bv
->block (block_index
);
2764 sym
= block_find_symbol (block
, name
, flags
, nullptr);
2766 error_in_psymtab_expansion (block_index
, name
.c_str (), cust
);
2767 gdb_assert (!TYPE_IS_OPAQUE (sym
->type ()));
2768 return sym
->type ();
2771 /* Subroutine of basic_lookup_transparent_type to simplify it.
2772 Look up the non-opaque definition of NAME in BLOCK_INDEX of OBJFILE.
2773 BLOCK_INDEX is either GLOBAL_BLOCK or STATIC_BLOCK. */
2775 static struct type
*
2776 basic_lookup_transparent_type_1 (struct objfile
*objfile
,
2777 enum block_enum block_index
,
2778 domain_search_flags flags
,
2779 const lookup_name_info
&name
)
2781 const struct blockvector
*bv
;
2782 const struct block
*block
;
2783 const struct symbol
*sym
;
2785 for (compunit_symtab
*cust
: objfile
->compunits ())
2787 bv
= cust
->blockvector ();
2788 block
= bv
->block (block_index
);
2789 sym
= block_find_symbol (block
, name
, flags
, nullptr);
2792 gdb_assert (!TYPE_IS_OPAQUE (sym
->type ()));
2793 return sym
->type ();
2800 /* The standard implementation of lookup_transparent_type. This code
2801 was modeled on lookup_symbol -- the parts not relevant to looking
2802 up types were just left out. In particular it's assumed here that
2803 types are available in STRUCT_DOMAIN and only in file-static or
2807 basic_lookup_transparent_type (const char *name
, domain_search_flags flags
)
2811 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
2813 /* Now search all the global symbols. Do the symtab's first, then
2814 check the psymtab's. If a psymtab indicates the existence
2815 of the desired name as a global, then do psymtab-to-symtab
2816 conversion on the fly and return the found symbol. */
2818 for (objfile
*objfile
: current_program_space
->objfiles ())
2820 t
= basic_lookup_transparent_type_1 (objfile
, GLOBAL_BLOCK
,
2821 flags
, lookup_name
);
2826 for (objfile
*objfile
: current_program_space
->objfiles ())
2828 t
= basic_lookup_transparent_type_quick (objfile
, GLOBAL_BLOCK
,
2829 flags
, lookup_name
);
2834 /* Now search the static file-level symbols.
2835 Not strictly correct, but more useful than an error.
2836 Do the symtab's first, then
2837 check the psymtab's. If a psymtab indicates the existence
2838 of the desired name as a file-level static, then do psymtab-to-symtab
2839 conversion on the fly and return the found symbol. */
2841 for (objfile
*objfile
: current_program_space
->objfiles ())
2843 t
= basic_lookup_transparent_type_1 (objfile
, STATIC_BLOCK
,
2844 flags
, lookup_name
);
2849 for (objfile
*objfile
: current_program_space
->objfiles ())
2851 t
= basic_lookup_transparent_type_quick (objfile
, STATIC_BLOCK
,
2852 flags
, lookup_name
);
2857 return (struct type
*) 0;
2863 iterate_over_symbols (const struct block
*block
,
2864 const lookup_name_info
&name
,
2865 const domain_search_flags domain
,
2866 gdb::function_view
<symbol_found_callback_ftype
> callback
)
2868 for (struct symbol
*sym
: block_iterator_range (block
, &name
))
2870 if (sym
->matches (domain
))
2872 struct block_symbol block_sym
= {sym
, block
};
2874 if (!callback (&block_sym
))
2884 iterate_over_symbols_terminated
2885 (const struct block
*block
,
2886 const lookup_name_info
&name
,
2887 const domain_search_flags domain
,
2888 gdb::function_view
<symbol_found_callback_ftype
> callback
)
2890 if (!iterate_over_symbols (block
, name
, domain
, callback
))
2892 struct block_symbol block_sym
= {nullptr, block
};
2893 return callback (&block_sym
);
2896 /* Find the compunit symtab associated with PC and SECTION.
2897 This will read in debug info as necessary. */
2899 struct compunit_symtab
*
2900 find_pc_sect_compunit_symtab (CORE_ADDR pc
, struct obj_section
*section
)
2902 struct compunit_symtab
*best_cust
= NULL
;
2903 CORE_ADDR best_cust_range
= 0;
2905 /* If we know that this is not a text address, return failure. This is
2906 necessary because we loop based on the block's high and low code
2907 addresses, which do not include the data ranges, and because
2908 we call find_pc_sect_psymtab which has a similar restriction based
2909 on the partial_symtab's texthigh and textlow. */
2910 bound_minimal_symbol msymbol
2911 = lookup_minimal_symbol_by_pc_section (pc
, section
);
2912 if (msymbol
.minsym
&& msymbol
.minsym
->data_p ())
2915 /* Search all symtabs for the one whose file contains our address, and which
2916 is the smallest of all the ones containing the address. This is designed
2917 to deal with a case like symtab a is at 0x1000-0x2000 and 0x3000-0x4000
2918 and symtab b is at 0x2000-0x3000. So the GLOBAL_BLOCK for a is from
2919 0x1000-0x4000, but for address 0x2345 we want to return symtab b.
2921 This happens for native ecoff format, where code from included files
2922 gets its own symtab. The symtab for the included file should have
2923 been read in already via the dependency mechanism.
2924 It might be swifter to create several symtabs with the same name
2925 like xcoff does (I'm not sure).
2927 It also happens for objfiles that have their functions reordered.
2928 For these, the symtab we are looking for is not necessarily read in. */
2930 for (objfile
*obj_file
: current_program_space
->objfiles ())
2932 for (compunit_symtab
*cust
: obj_file
->compunits ())
2934 const struct blockvector
*bv
= cust
->blockvector ();
2935 const struct block
*global_block
= bv
->global_block ();
2936 CORE_ADDR start
= global_block
->start ();
2937 CORE_ADDR end
= global_block
->end ();
2938 bool in_range_p
= start
<= pc
&& pc
< end
;
2942 if (bv
->map () != nullptr)
2944 if (bv
->map ()->find (pc
) == nullptr)
2950 CORE_ADDR range
= end
- start
;
2951 if (best_cust
!= nullptr
2952 && range
>= best_cust_range
)
2953 /* Cust doesn't have a smaller range than best_cust, skip it. */
2956 /* For an objfile that has its functions reordered,
2957 find_pc_psymtab will find the proper partial symbol table
2958 and we simply return its corresponding symtab. */
2959 /* In order to better support objfiles that contain both
2960 stabs and coff debugging info, we continue on if a psymtab
2962 struct compunit_symtab
*result
2963 = obj_file
->find_pc_sect_compunit_symtab (msymbol
, pc
,
2965 if (result
!= nullptr)
2970 struct symbol
*found_sym
= nullptr;
2972 for (int b_index
= GLOBAL_BLOCK
;
2973 b_index
<= STATIC_BLOCK
&& found_sym
== nullptr;
2976 const struct block
*b
= bv
->block (b_index
);
2977 for (struct symbol
*sym
: block_iterator_range (b
))
2979 if (matching_obj_sections (sym
->obj_section (obj_file
),
2987 if (found_sym
== nullptr)
2988 continue; /* No symbol in this symtab matches
2992 /* Cust is best found so far, save it. */
2994 best_cust_range
= range
;
2998 if (best_cust
!= NULL
)
3001 /* Not found in symtabs, search the "quick" symtabs (e.g. psymtabs). */
3003 for (objfile
*objf
: current_program_space
->objfiles ())
3005 struct compunit_symtab
*result
3006 = objf
->find_pc_sect_compunit_symtab (msymbol
, pc
, section
, 1);
3014 /* Find the compunit symtab associated with PC.
3015 This will read in debug info as necessary.
3016 Backward compatibility, no section. */
3018 struct compunit_symtab
*
3019 find_pc_compunit_symtab (CORE_ADDR pc
)
3021 return find_pc_sect_compunit_symtab (pc
, find_pc_mapped_section (pc
));
3027 find_symbol_at_address (CORE_ADDR address
)
3029 /* A helper function to search a given symtab for a symbol matching
3031 auto search_symtab
= [] (compunit_symtab
*symtab
, CORE_ADDR addr
) -> symbol
*
3033 const struct blockvector
*bv
= symtab
->blockvector ();
3035 for (int i
= GLOBAL_BLOCK
; i
<= STATIC_BLOCK
; ++i
)
3037 const struct block
*b
= bv
->block (i
);
3039 for (struct symbol
*sym
: block_iterator_range (b
))
3041 if (sym
->aclass () == LOC_STATIC
3042 && sym
->value_address () == addr
)
3049 for (objfile
*objfile
: current_program_space
->objfiles ())
3051 /* If this objfile was read with -readnow, then we need to
3052 search the symtabs directly. */
3053 if ((objfile
->flags
& OBJF_READNOW
) != 0)
3055 for (compunit_symtab
*symtab
: objfile
->compunits ())
3057 struct symbol
*sym
= search_symtab (symtab
, address
);
3064 struct compunit_symtab
*symtab
3065 = objfile
->find_compunit_symtab_by_address (address
);
3068 struct symbol
*sym
= search_symtab (symtab
, address
);
3080 /* Find the source file and line number for a given PC value and SECTION.
3081 Return a structure containing a symtab pointer, a line number,
3082 and a pc range for the entire source line.
3083 The value's .pc field is NOT the specified pc.
3084 NOTCURRENT nonzero means, if specified pc is on a line boundary,
3085 use the line that ends there. Otherwise, in that case, the line
3086 that begins there is used. */
3088 /* The big complication here is that a line may start in one file, and end just
3089 before the start of another file. This usually occurs when you #include
3090 code in the middle of a subroutine. To properly find the end of a line's PC
3091 range, we must search all symtabs associated with this compilation unit, and
3092 find the one whose first PC is closer than that of the next line in this
3095 struct symtab_and_line
3096 find_pc_sect_line (CORE_ADDR pc
, struct obj_section
*section
, int notcurrent
)
3098 struct compunit_symtab
*cust
;
3101 const linetable_entry
*item
;
3102 const struct blockvector
*bv
;
3104 /* Info on best line seen so far, and where it starts, and its file. */
3106 const linetable_entry
*best
= NULL
;
3107 CORE_ADDR best_end
= 0;
3108 struct symtab
*best_symtab
= 0;
3110 /* Store here the first line number
3111 of a file which contains the line at the smallest pc after PC.
3112 If we don't find a line whose range contains PC,
3113 we will use a line one less than this,
3114 with a range from the start of that file to the first line's pc. */
3115 const linetable_entry
*alt
= NULL
;
3117 /* Info on best line seen in this file. */
3119 const linetable_entry
*prev
;
3121 /* If this pc is not from the current frame,
3122 it is the address of the end of a call instruction.
3123 Quite likely that is the start of the following statement.
3124 But what we want is the statement containing the instruction.
3125 Fudge the pc to make sure we get that. */
3127 /* It's tempting to assume that, if we can't find debugging info for
3128 any function enclosing PC, that we shouldn't search for line
3129 number info, either. However, GAS can emit line number info for
3130 assembly files --- very helpful when debugging hand-written
3131 assembly code. In such a case, we'd have no debug info for the
3132 function, but we would have line info. */
3137 /* elz: added this because this function returned the wrong
3138 information if the pc belongs to a stub (import/export)
3139 to call a shlib function. This stub would be anywhere between
3140 two functions in the target, and the line info was erroneously
3141 taken to be the one of the line before the pc. */
3143 /* RT: Further explanation:
3145 * We have stubs (trampolines) inserted between procedures.
3147 * Example: "shr1" exists in a shared library, and a "shr1" stub also
3148 * exists in the main image.
3150 * In the minimal symbol table, we have a bunch of symbols
3151 * sorted by start address. The stubs are marked as "trampoline",
3152 * the others appear as text. E.g.:
3154 * Minimal symbol table for main image
3155 * main: code for main (text symbol)
3156 * shr1: stub (trampoline symbol)
3157 * foo: code for foo (text symbol)
3159 * Minimal symbol table for "shr1" image:
3161 * shr1: code for shr1 (text symbol)
3164 * So the code below is trying to detect if we are in the stub
3165 * ("shr1" stub), and if so, find the real code ("shr1" trampoline),
3166 * and if found, do the symbolization from the real-code address
3167 * rather than the stub address.
3169 * Assumptions being made about the minimal symbol table:
3170 * 1. lookup_minimal_symbol_by_pc() will return a trampoline only
3171 * if we're really in the trampoline.s If we're beyond it (say
3172 * we're in "foo" in the above example), it'll have a closer
3173 * symbol (the "foo" text symbol for example) and will not
3174 * return the trampoline.
3175 * 2. lookup_minimal_symbol_text() will find a real text symbol
3176 * corresponding to the trampoline, and whose address will
3177 * be different than the trampoline address. I put in a sanity
3178 * check for the address being the same, to avoid an
3179 * infinite recursion.
3181 bound_minimal_symbol msymbol
= lookup_minimal_symbol_by_pc (pc
);
3182 if (msymbol
.minsym
!= NULL
)
3183 if (msymbol
.minsym
->type () == mst_solib_trampoline
)
3185 bound_minimal_symbol mfunsym
3186 = lookup_minimal_symbol_text (current_program_space
,
3187 msymbol
.minsym
->linkage_name (),
3190 if (mfunsym
.minsym
== NULL
)
3191 /* I eliminated this warning since it is coming out
3192 * in the following situation:
3193 * gdb shmain // test program with shared libraries
3194 * (gdb) break shr1 // function in shared lib
3195 * Warning: In stub for ...
3196 * In the above situation, the shared lib is not loaded yet,
3197 * so of course we can't find the real func/line info,
3198 * but the "break" still works, and the warning is annoying.
3199 * So I commented out the warning. RT */
3200 /* warning ("In stub for %s; unable to find real function/line info",
3201 msymbol->linkage_name ()); */
3204 else if (mfunsym
.value_address ()
3205 == msymbol
.value_address ())
3206 /* Avoid infinite recursion */
3207 /* See above comment about why warning is commented out. */
3208 /* warning ("In stub for %s; unable to find real function/line info",
3209 msymbol->linkage_name ()); */
3214 /* Detect an obvious case of infinite recursion. If this
3215 should occur, we'd like to know about it, so error out,
3217 if (mfunsym
.value_address () == pc
)
3218 internal_error (_("Infinite recursion detected in find_pc_sect_line;"
3219 "please file a bug report"));
3221 return find_pc_line (mfunsym
.value_address (), 0);
3225 symtab_and_line val
;
3226 val
.pspace
= current_program_space
;
3228 cust
= find_pc_sect_compunit_symtab (pc
, section
);
3231 /* If no symbol information, return previous pc. */
3238 bv
= cust
->blockvector ();
3239 struct objfile
*objfile
= cust
->objfile ();
3241 /* Look at all the symtabs that share this blockvector.
3242 They all have the same apriori range, that we found was right;
3243 but they have different line tables. */
3245 for (symtab
*iter_s
: cust
->filetabs ())
3247 /* Find the best line in this symtab. */
3248 l
= iter_s
->linetable ();
3254 /* I think len can be zero if the symtab lacks line numbers
3255 (e.g. gcc -g1). (Either that or the LINETABLE is NULL;
3256 I'm not sure which, and maybe it depends on the symbol
3262 item
= l
->item
; /* Get first line info. */
3264 /* Is this file's first line closer than the first lines of other files?
3265 If so, record this file, and its first line, as best alternate. */
3266 if (item
->pc (objfile
) > pc
3267 && (!alt
|| item
->unrelocated_pc () < alt
->unrelocated_pc ()))
3270 auto pc_compare
= [] (const unrelocated_addr
&comp_pc
,
3271 const struct linetable_entry
& lhs
)
3273 return comp_pc
< lhs
.unrelocated_pc ();
3276 const linetable_entry
*first
= item
;
3277 const linetable_entry
*last
= item
+ len
;
3278 item
= (std::upper_bound
3280 unrelocated_addr (pc
- objfile
->text_section_offset ()),
3284 prev
= item
- 1; /* Found a matching item. */
3285 /* At this point, prev is a line whose address is <= pc. However, we
3286 don't know if ITEM is pointing to the same statement or not. */
3287 while (item
!= last
&& prev
->line
== item
->line
&& !item
->is_stmt
)
3291 /* At this point, prev points at the line whose start addr is <= pc, and
3292 item points at the next statement. If we ran off the end of the linetable
3293 (pc >= start of the last line), then prev == item. If pc < start of
3294 the first line, prev will not be set. */
3296 /* Is this file's best line closer than the best in the other files?
3297 If so, record this file, and its best line, as best so far. Don't
3298 save prev if it represents the end of a function (i.e. line number
3299 0) instead of a real line. */
3301 if (prev
&& prev
->line
3302 && (!best
|| prev
->unrelocated_pc () > best
->unrelocated_pc ()))
3305 best_symtab
= iter_s
;
3307 /* If during the binary search we land on a non-statement entry,
3308 scan backward through entries at the same address to see if
3309 there is an entry marked as is-statement. In theory this
3310 duplication should have been removed from the line table
3311 during construction, this is just a double check. If the line
3312 table has had the duplication removed then this should be
3316 const linetable_entry
*tmp
= best
;
3318 && (tmp
- 1)->unrelocated_pc () == tmp
->unrelocated_pc ()
3319 && (tmp
- 1)->line
!= 0 && !tmp
->is_stmt
)
3325 /* Discard BEST_END if it's before the PC of the current BEST. */
3326 if (best_end
<= best
->pc (objfile
))
3330 /* If another line (denoted by ITEM) is in the linetable and its
3331 PC is after BEST's PC, but before the current BEST_END, then
3332 use ITEM's PC as the new best_end. */
3333 if (best
&& item
< last
3334 && item
->unrelocated_pc () > best
->unrelocated_pc ()
3335 && (best_end
== 0 || best_end
> item
->pc (objfile
)))
3336 best_end
= item
->pc (objfile
);
3341 /* If we didn't find any line number info, just return zeros.
3342 We used to return alt->line - 1 here, but that could be
3343 anywhere; if we don't have line number info for this PC,
3344 don't make some up. */
3347 else if (best
->line
== 0)
3349 /* If our best fit is in a range of PC's for which no line
3350 number info is available (line number is zero) then we didn't
3351 find any valid line information. */
3356 val
.is_stmt
= best
->is_stmt
;
3357 val
.symtab
= best_symtab
;
3358 val
.line
= best
->line
;
3359 val
.pc
= best
->pc (objfile
);
3360 if (best_end
&& (!alt
|| best_end
< alt
->pc (objfile
)))
3363 val
.end
= alt
->pc (objfile
);
3365 val
.end
= bv
->global_block ()->end ();
3367 val
.section
= section
;
3371 /* Backward compatibility (no section). */
3373 struct symtab_and_line
3374 find_pc_line (CORE_ADDR pc
, int notcurrent
)
3376 struct obj_section
*section
;
3378 section
= find_pc_overlay (pc
);
3379 if (!pc_in_unmapped_range (pc
, section
))
3380 return find_pc_sect_line (pc
, section
, notcurrent
);
3382 /* If the original PC was an unmapped address then we translate this to a
3383 mapped address in order to lookup the sal. However, as the user
3384 passed us an unmapped address it makes more sense to return a result
3385 that has the pc and end fields translated to unmapped addresses. */
3386 pc
= overlay_mapped_address (pc
, section
);
3387 symtab_and_line sal
= find_pc_sect_line (pc
, section
, notcurrent
);
3388 sal
.pc
= overlay_unmapped_address (sal
.pc
, section
);
3389 sal
.end
= overlay_unmapped_address (sal
.end
, section
);
3393 /* Compare two symtab_and_line entries. Return true if both have
3394 the same line number and the same symtab pointer. That means we
3395 are dealing with two entries from the same line and from the same
3398 Return false otherwise. */
3401 sal_line_symtab_matches_p (const symtab_and_line
&sal1
,
3402 const symtab_and_line
&sal2
)
3404 return sal1
.line
== sal2
.line
&& sal1
.symtab
== sal2
.symtab
;
3409 std::optional
<CORE_ADDR
>
3410 find_line_range_start (CORE_ADDR pc
)
3412 struct symtab_and_line current_sal
= find_pc_line (pc
, 0);
3414 if (current_sal
.line
== 0)
3417 struct symtab_and_line prev_sal
= find_pc_line (current_sal
.pc
- 1, 0);
3419 /* If the previous entry is for a different line, that means we are already
3420 at the entry with the start PC for this line. */
3421 if (!sal_line_symtab_matches_p (prev_sal
, current_sal
))
3422 return current_sal
.pc
;
3424 /* Otherwise, keep looking for entries for the same line but with
3430 prev_pc
= prev_sal
.pc
;
3432 prev_sal
= find_pc_line (prev_pc
- 1, 0);
3434 /* Did we notice a line change? If so, we are done searching. */
3435 if (!sal_line_symtab_matches_p (prev_sal
, current_sal
))
3445 find_pc_line_symtab (CORE_ADDR pc
)
3447 struct symtab_and_line sal
;
3449 /* This always passes zero for NOTCURRENT to find_pc_line.
3450 There are currently no callers that ever pass non-zero. */
3451 sal
= find_pc_line (pc
, 0);
3458 find_line_symtab (symtab
*sym_tab
, int line
, int *index
)
3460 int exact
= 0; /* Initialized here to avoid a compiler warning. */
3462 /* BEST_INDEX and BEST_LINETABLE identify the smallest linenumber > LINE
3466 const struct linetable
*best_linetable
;
3467 struct symtab
*best_symtab
;
3469 /* First try looking it up in the given symtab. */
3470 best_linetable
= sym_tab
->linetable ();
3471 best_symtab
= sym_tab
;
3472 best_index
= find_line_common (best_linetable
, line
, &exact
, 0);
3473 if (best_index
< 0 || !exact
)
3475 /* Didn't find an exact match. So we better keep looking for
3476 another symtab with the same name. In the case of xcoff,
3477 multiple csects for one source file (produced by IBM's FORTRAN
3478 compiler) produce multiple symtabs (this is unavoidable
3479 assuming csects can be at arbitrary places in memory and that
3480 the GLOBAL_BLOCK of a symtab has a begin and end address). */
3482 /* BEST is the smallest linenumber > LINE so far seen,
3483 or 0 if none has been seen so far.
3484 BEST_INDEX and BEST_LINETABLE identify the item for it. */
3487 if (best_index
>= 0)
3488 best
= best_linetable
->item
[best_index
].line
;
3492 for (objfile
*objfile
: current_program_space
->objfiles ())
3493 objfile
->expand_symtabs_with_fullname (symtab_to_fullname (sym_tab
));
3495 for (objfile
*objfile
: current_program_space
->objfiles ())
3497 for (compunit_symtab
*cu
: objfile
->compunits ())
3499 for (symtab
*s
: cu
->filetabs ())
3501 const struct linetable
*l
;
3504 if (FILENAME_CMP (sym_tab
->filename
, s
->filename
) != 0)
3506 if (FILENAME_CMP (symtab_to_fullname (sym_tab
),
3507 symtab_to_fullname (s
)) != 0)
3509 l
= s
->linetable ();
3510 ind
= find_line_common (l
, line
, &exact
, 0);
3520 if (best
== 0 || l
->item
[ind
].line
< best
)
3522 best
= l
->item
[ind
].line
;
3537 *index
= best_index
;
3542 /* Given SYMTAB, returns all the PCs function in the symtab that
3543 exactly match LINE. Returns an empty vector if there are no exact
3544 matches, but updates BEST_ITEM in this case. */
3546 std::vector
<CORE_ADDR
>
3547 find_pcs_for_symtab_line (struct symtab
*symtab
, int line
,
3548 const linetable_entry
**best_item
)
3551 std::vector
<CORE_ADDR
> result
;
3552 struct objfile
*objfile
= symtab
->compunit ()->objfile ();
3554 /* First, collect all the PCs that are at this line. */
3560 idx
= find_line_common (symtab
->linetable (), line
, &was_exact
,
3567 const linetable_entry
*item
= &symtab
->linetable ()->item
[idx
];
3569 if (*best_item
== NULL
3570 || (item
->line
< (*best_item
)->line
&& item
->is_stmt
))
3576 result
.push_back (symtab
->linetable ()->item
[idx
].pc (objfile
));
3584 /* Set the PC value for a given source file and line number and return true.
3585 Returns false for invalid line number (and sets the PC to 0).
3586 The source file is specified with a struct symtab. */
3589 find_line_pc (struct symtab
*symtab
, int line
, CORE_ADDR
*pc
)
3591 const struct linetable
*l
;
3598 symtab
= find_line_symtab (symtab
, line
, &ind
);
3601 l
= symtab
->linetable ();
3602 *pc
= l
->item
[ind
].pc (symtab
->compunit ()->objfile ());
3609 /* Find the range of pc values in a line.
3610 Store the starting pc of the line into *STARTPTR
3611 and the ending pc (start of next line) into *ENDPTR.
3612 Returns true to indicate success.
3613 Returns false if could not find the specified line. */
3616 find_line_pc_range (struct symtab_and_line sal
, CORE_ADDR
*startptr
,
3619 CORE_ADDR startaddr
;
3620 struct symtab_and_line found_sal
;
3623 if (startaddr
== 0 && !find_line_pc (sal
.symtab
, sal
.line
, &startaddr
))
3626 /* This whole function is based on address. For example, if line 10 has
3627 two parts, one from 0x100 to 0x200 and one from 0x300 to 0x400, then
3628 "info line *0x123" should say the line goes from 0x100 to 0x200
3629 and "info line *0x355" should say the line goes from 0x300 to 0x400.
3630 This also insures that we never give a range like "starts at 0x134
3631 and ends at 0x12c". */
3633 found_sal
= find_pc_sect_line (startaddr
, sal
.section
, 0);
3634 if (found_sal
.line
!= sal
.line
)
3636 /* The specified line (sal) has zero bytes. */
3637 *startptr
= found_sal
.pc
;
3638 *endptr
= found_sal
.pc
;
3642 *startptr
= found_sal
.pc
;
3643 *endptr
= found_sal
.end
;
3648 /* Given a line table and a line number, return the index into the line
3649 table for the pc of the nearest line whose number is >= the specified one.
3650 Return -1 if none is found. The value is >= 0 if it is an index.
3651 START is the index at which to start searching the line table.
3653 Set *EXACT_MATCH nonzero if the value returned is an exact match. */
3656 find_line_common (const linetable
*l
, int lineno
,
3657 int *exact_match
, int start
)
3662 /* BEST is the smallest linenumber > LINENO so far seen,
3663 or 0 if none has been seen so far.
3664 BEST_INDEX identifies the item for it. */
3666 int best_index
= -1;
3677 for (i
= start
; i
< len
; i
++)
3679 const linetable_entry
*item
= &(l
->item
[i
]);
3681 /* Ignore non-statements. */
3685 if (item
->line
== lineno
)
3687 /* Return the first (lowest address) entry which matches. */
3692 if (item
->line
> lineno
&& (best
== 0 || item
->line
< best
))
3699 /* If we got here, we didn't get an exact match. */
3704 find_pc_line_pc_range (CORE_ADDR pc
, CORE_ADDR
*startptr
, CORE_ADDR
*endptr
)
3706 struct symtab_and_line sal
;
3708 sal
= find_pc_line (pc
, 0);
3711 return sal
.symtab
!= 0;
3714 /* Helper for find_function_start_sal. Does most of the work, except
3715 setting the sal's symbol. */
3717 static symtab_and_line
3718 find_function_start_sal_1 (CORE_ADDR func_addr
, obj_section
*section
,
3721 symtab_and_line sal
= find_pc_sect_line (func_addr
, section
, 0);
3723 if (funfirstline
&& sal
.symtab
!= NULL
3724 && (sal
.symtab
->compunit ()->locations_valid ()
3725 || sal
.symtab
->language () == language_asm
))
3727 struct gdbarch
*gdbarch
= sal
.symtab
->compunit ()->objfile ()->arch ();
3730 if (gdbarch_skip_entrypoint_p (gdbarch
))
3731 sal
.pc
= gdbarch_skip_entrypoint (gdbarch
, sal
.pc
);
3735 /* We always should have a line for the function start address.
3736 If we don't, something is odd. Create a plain SAL referring
3737 just the PC and hope that skip_prologue_sal (if requested)
3738 can find a line number for after the prologue. */
3739 if (sal
.pc
< func_addr
)
3742 sal
.pspace
= current_program_space
;
3744 sal
.section
= section
;
3748 skip_prologue_sal (&sal
);
3756 find_function_start_sal (CORE_ADDR func_addr
, obj_section
*section
,
3760 = find_function_start_sal_1 (func_addr
, section
, funfirstline
);
3762 /* find_function_start_sal_1 does a linetable search, so it finds
3763 the symtab and linenumber, but not a symbol. Fill in the
3764 function symbol too. */
3765 sal
.symbol
= find_pc_sect_containing_function (sal
.pc
, sal
.section
);
3773 find_function_start_sal (symbol
*sym
, bool funfirstline
)
3776 = find_function_start_sal_1 (sym
->value_block ()->entry_pc (),
3777 sym
->obj_section (sym
->objfile ()),
3784 /* Given a function start address FUNC_ADDR and SYMTAB, find the first
3785 address for that function that has an entry in SYMTAB's line info
3786 table. If such an entry cannot be found, return FUNC_ADDR
3790 skip_prologue_using_lineinfo (CORE_ADDR func_addr
, struct symtab
*symtab
)
3792 CORE_ADDR func_start
, func_end
;
3793 const struct linetable
*l
;
3796 /* Give up if this symbol has no lineinfo table. */
3797 l
= symtab
->linetable ();
3801 /* Get the range for the function's PC values, or give up if we
3802 cannot, for some reason. */
3803 if (!find_pc_partial_function (func_addr
, NULL
, &func_start
, &func_end
))
3806 struct objfile
*objfile
= symtab
->compunit ()->objfile ();
3808 /* Linetable entries are ordered by PC values, see the commentary in
3809 symtab.h where `struct linetable' is defined. Thus, the first
3810 entry whose PC is in the range [FUNC_START..FUNC_END[ is the
3811 address we are looking for. */
3812 for (i
= 0; i
< l
->nitems
; i
++)
3814 const linetable_entry
*item
= &(l
->item
[i
]);
3815 CORE_ADDR item_pc
= item
->pc (objfile
);
3817 /* Don't use line numbers of zero, they mark special entries in
3818 the table. See the commentary on symtab.h before the
3819 definition of struct linetable. */
3820 if (item
->line
> 0 && func_start
<= item_pc
&& item_pc
< func_end
)
3827 /* Try to locate the address where a breakpoint should be placed past the
3828 prologue of function starting at FUNC_ADDR using the line table.
3830 Return the address associated with the first entry in the line-table for
3831 the function starting at FUNC_ADDR which has prologue_end set to true if
3832 such entry exist, otherwise return an empty optional. */
3834 static std::optional
<CORE_ADDR
>
3835 skip_prologue_using_linetable (CORE_ADDR func_addr
)
3837 CORE_ADDR start_pc
, end_pc
;
3839 if (!find_pc_partial_function (func_addr
, nullptr, &start_pc
, &end_pc
))
3842 const struct symtab_and_line prologue_sal
= find_pc_line (start_pc
, 0);
3843 if (prologue_sal
.symtab
!= nullptr
3844 && prologue_sal
.symtab
->language () != language_asm
)
3846 const linetable
*linetable
= prologue_sal
.symtab
->linetable ();
3848 struct objfile
*objfile
= prologue_sal
.symtab
->compunit ()->objfile ();
3850 unrelocated_addr unrel_start
3851 = unrelocated_addr (start_pc
- objfile
->text_section_offset ());
3852 unrelocated_addr unrel_end
3853 = unrelocated_addr (end_pc
- objfile
->text_section_offset ());
3855 auto it
= std::lower_bound
3856 (linetable
->item
, linetable
->item
+ linetable
->nitems
, unrel_start
,
3857 [] (const linetable_entry
<e
, unrelocated_addr pc
)
3859 return lte
.unrelocated_pc () < pc
;
3863 (it
< linetable
->item
+ linetable
->nitems
3864 && it
->unrelocated_pc () < unrel_end
);
3866 if (it
->prologue_end
)
3867 return {it
->pc (objfile
)};
3873 /* Adjust SAL to the first instruction past the function prologue.
3874 If the PC was explicitly specified, the SAL is not changed.
3875 If the line number was explicitly specified then the SAL can still be
3876 updated, unless the language for SAL is assembler, in which case the SAL
3877 will be left unchanged.
3878 If SAL is already past the prologue, then do nothing. */
3881 skip_prologue_sal (struct symtab_and_line
*sal
)
3884 struct symtab_and_line start_sal
;
3885 CORE_ADDR pc
, saved_pc
;
3886 struct obj_section
*section
;
3888 struct objfile
*objfile
;
3889 struct gdbarch
*gdbarch
;
3890 const struct block
*b
, *function_block
;
3891 int force_skip
, skip
;
3893 /* Do not change the SAL if PC was specified explicitly. */
3894 if (sal
->explicit_pc
)
3897 /* In assembly code, if the user asks for a specific line then we should
3898 not adjust the SAL. The user already has instruction level
3899 visibility in this case, so selecting a line other than one requested
3900 is likely to be the wrong choice. */
3901 if (sal
->symtab
!= nullptr
3902 && sal
->explicit_line
3903 && sal
->symtab
->language () == language_asm
)
3906 scoped_restore_current_pspace_and_thread restore_pspace_thread
;
3908 switch_to_program_space_and_thread (sal
->pspace
);
3910 sym
= find_pc_sect_function (sal
->pc
, sal
->section
);
3913 objfile
= sym
->objfile ();
3914 pc
= sym
->value_block ()->entry_pc ();
3915 section
= sym
->obj_section (objfile
);
3916 name
= sym
->linkage_name ();
3920 bound_minimal_symbol msymbol
3921 = lookup_minimal_symbol_by_pc_section (sal
->pc
, sal
->section
);
3923 if (msymbol
.minsym
== NULL
)
3926 objfile
= msymbol
.objfile
;
3927 pc
= msymbol
.value_address ();
3928 section
= msymbol
.minsym
->obj_section (objfile
);
3929 name
= msymbol
.minsym
->linkage_name ();
3932 gdbarch
= objfile
->arch ();
3934 /* Process the prologue in two passes. In the first pass try to skip the
3935 prologue (SKIP is true) and verify there is a real need for it (indicated
3936 by FORCE_SKIP). If no such reason was found run a second pass where the
3937 prologue is not skipped (SKIP is false). */
3942 /* Be conservative - allow direct PC (without skipping prologue) only if we
3943 have proven the CU (Compilation Unit) supports it. sal->SYMTAB does not
3944 have to be set by the caller so we use SYM instead. */
3946 && sym
->symtab ()->compunit ()->locations_valid ())
3954 /* Check if the compiler explicitly indicated where a breakpoint should
3955 be placed to skip the prologue. */
3956 if (!ignore_prologue_end_flag
&& skip
)
3958 std::optional
<CORE_ADDR
> linetable_pc
3959 = skip_prologue_using_linetable (pc
);
3963 start_sal
= find_pc_sect_line (pc
, section
, 0);
3969 /* If the function is in an unmapped overlay, use its unmapped LMA address,
3970 so that gdbarch_skip_prologue has something unique to work on. */
3971 if (section_is_overlay (section
) && !section_is_mapped (section
))
3972 pc
= overlay_unmapped_address (pc
, section
);
3974 /* Skip "first line" of function (which is actually its prologue). */
3975 pc
+= gdbarch_deprecated_function_start_offset (gdbarch
);
3976 if (gdbarch_skip_entrypoint_p (gdbarch
))
3977 pc
= gdbarch_skip_entrypoint (gdbarch
, pc
);
3979 pc
= gdbarch_skip_prologue_noexcept (gdbarch
, pc
);
3981 /* For overlays, map pc back into its mapped VMA range. */
3982 pc
= overlay_mapped_address (pc
, section
);
3984 /* Calculate line number. */
3985 start_sal
= find_pc_sect_line (pc
, section
, 0);
3987 /* Check if gdbarch_skip_prologue left us in mid-line, and the next
3988 line is still part of the same function. */
3989 if (skip
&& start_sal
.pc
!= pc
3990 && (sym
? (sym
->value_block ()->entry_pc () <= start_sal
.end
3991 && start_sal
.end
< sym
->value_block()->end ())
3992 : (lookup_minimal_symbol_by_pc_section (start_sal
.end
, section
).minsym
3993 == lookup_minimal_symbol_by_pc_section (pc
, section
).minsym
)))
3995 /* First pc of next line */
3997 /* Recalculate the line number (might not be N+1). */
3998 start_sal
= find_pc_sect_line (pc
, section
, 0);
4001 /* On targets with executable formats that don't have a concept of
4002 constructors (ELF with .init has, PE doesn't), gcc emits a call
4003 to `__main' in `main' between the prologue and before user
4005 if (gdbarch_skip_main_prologue_p (gdbarch
)
4006 && name
&& strcmp_iw (name
, "main") == 0)
4008 pc
= gdbarch_skip_main_prologue (gdbarch
, pc
);
4009 /* Recalculate the line number (might not be N+1). */
4010 start_sal
= find_pc_sect_line (pc
, section
, 0);
4014 while (!force_skip
&& skip
--);
4016 /* If we still don't have a valid source line, try to find the first
4017 PC in the lineinfo table that belongs to the same function. This
4018 happens with COFF debug info, which does not seem to have an
4019 entry in lineinfo table for the code after the prologue which has
4020 no direct relation to source. For example, this was found to be
4021 the case with the DJGPP target using "gcc -gcoff" when the
4022 compiler inserted code after the prologue to make sure the stack
4024 if (!force_skip
&& sym
&& start_sal
.symtab
== NULL
)
4026 pc
= skip_prologue_using_lineinfo (pc
, sym
->symtab ());
4027 /* Recalculate the line number. */
4028 start_sal
= find_pc_sect_line (pc
, section
, 0);
4031 /* If we're already past the prologue, leave SAL unchanged. Otherwise
4032 forward SAL to the end of the prologue. */
4037 sal
->section
= section
;
4038 sal
->symtab
= start_sal
.symtab
;
4039 sal
->line
= start_sal
.line
;
4040 sal
->end
= start_sal
.end
;
4042 /* Check if we are now inside an inlined function. If we can,
4043 use the call site of the function instead. */
4044 b
= block_for_pc_sect (sal
->pc
, sal
->section
);
4045 function_block
= NULL
;
4048 if (b
->function () != NULL
&& b
->inlined_p ())
4050 else if (b
->function () != NULL
)
4052 b
= b
->superblock ();
4054 if (function_block
!= NULL
4055 && function_block
->function ()->line () != 0)
4057 sal
->line
= function_block
->function ()->line ();
4058 sal
->symtab
= function_block
->function ()->symtab ();
4062 /* Given PC at the function's start address, attempt to find the
4063 prologue end using SAL information. Return zero if the skip fails.
4065 A non-optimized prologue traditionally has one SAL for the function
4066 and a second for the function body. A single line function has
4067 them both pointing at the same line.
4069 An optimized prologue is similar but the prologue may contain
4070 instructions (SALs) from the instruction body. Need to skip those
4071 while not getting into the function body.
4073 The functions end point and an increasing SAL line are used as
4074 indicators of the prologue's endpoint.
4076 This code is based on the function refine_prologue_limit
4080 skip_prologue_using_sal (struct gdbarch
*gdbarch
, CORE_ADDR func_addr
)
4082 struct symtab_and_line prologue_sal
;
4085 const struct block
*bl
;
4087 /* Get an initial range for the function. */
4088 find_pc_partial_function (func_addr
, NULL
, &start_pc
, &end_pc
);
4089 start_pc
+= gdbarch_deprecated_function_start_offset (gdbarch
);
4091 prologue_sal
= find_pc_line (start_pc
, 0);
4092 if (prologue_sal
.line
!= 0)
4094 /* For languages other than assembly, treat two consecutive line
4095 entries at the same address as a zero-instruction prologue.
4096 The GNU assembler emits separate line notes for each instruction
4097 in a multi-instruction macro, but compilers generally will not
4099 if (prologue_sal
.symtab
->language () != language_asm
)
4101 struct objfile
*objfile
4102 = prologue_sal
.symtab
->compunit ()->objfile ();
4103 const linetable
*linetable
= prologue_sal
.symtab
->linetable ();
4104 gdb_assert (linetable
->nitems
> 0);
4107 /* Skip any earlier lines, and any end-of-sequence marker
4108 from a previous function. */
4109 while (idx
+ 1 < linetable
->nitems
4110 && (linetable
->item
[idx
].pc (objfile
) != prologue_sal
.pc
4111 || linetable
->item
[idx
].line
== 0))
4114 if (idx
+ 1 < linetable
->nitems
4115 && linetable
->item
[idx
+1].line
!= 0
4116 && linetable
->item
[idx
+1].pc (objfile
) == start_pc
)
4120 /* If there is only one sal that covers the entire function,
4121 then it is probably a single line function, like
4123 if (prologue_sal
.end
>= end_pc
)
4126 while (prologue_sal
.end
< end_pc
)
4128 struct symtab_and_line sal
;
4130 sal
= find_pc_line (prologue_sal
.end
, 0);
4133 /* Assume that a consecutive SAL for the same (or larger)
4134 line mark the prologue -> body transition. */
4135 if (sal
.line
>= prologue_sal
.line
)
4137 /* Likewise if we are in a different symtab altogether
4138 (e.g. within a file included via #include). */
4139 if (sal
.symtab
!= prologue_sal
.symtab
)
4142 /* The line number is smaller. Check that it's from the
4143 same function, not something inlined. If it's inlined,
4144 then there is no point comparing the line numbers. */
4145 bl
= block_for_pc (prologue_sal
.end
);
4148 if (bl
->inlined_p ())
4150 if (bl
->function ())
4155 bl
= bl
->superblock ();
4160 /* The case in which compiler's optimizer/scheduler has
4161 moved instructions into the prologue. We look ahead in
4162 the function looking for address ranges whose
4163 corresponding line number is less the first one that we
4164 found for the function. This is more conservative then
4165 refine_prologue_limit which scans a large number of SALs
4166 looking for any in the prologue. */
4171 if (prologue_sal
.end
< end_pc
)
4172 /* Return the end of this line, or zero if we could not find a
4174 return prologue_sal
.end
;
4176 /* Don't return END_PC, which is past the end of the function. */
4177 return prologue_sal
.pc
;
4182 std::optional
<CORE_ADDR
>
4183 find_epilogue_using_linetable (CORE_ADDR func_addr
)
4185 CORE_ADDR start_pc
, end_pc
;
4187 if (!find_pc_partial_function (func_addr
, nullptr, &start_pc
, &end_pc
))
4190 /* While the standard allows for multiple points marked with epilogue_begin
4191 in the same function, for performance reasons, this function will only
4192 find the last address that sets this flag for a given block.
4194 The lines of a function can be described by several line tables in case
4195 there are different files involved. There's a corner case where a
4196 function epilogue is in a different file than a function start, and using
4197 start_pc as argument to find_pc_line will mean we won't find the
4198 epilogue. Instead, use "end_pc - 1" to maximize our chances of picking
4199 the line table containing an epilogue. */
4200 const struct symtab_and_line sal
= find_pc_line (end_pc
- 1, 0);
4201 if (sal
.symtab
!= nullptr && sal
.symtab
->language () != language_asm
)
4203 struct objfile
*objfile
= sal
.symtab
->compunit ()->objfile ();
4204 unrelocated_addr unrel_start
4205 = unrelocated_addr (start_pc
- objfile
->text_section_offset ());
4206 unrelocated_addr unrel_end
4207 = unrelocated_addr (end_pc
- objfile
->text_section_offset ());
4209 const linetable
*linetable
= sal
.symtab
->linetable ();
4210 if (linetable
== nullptr || linetable
->nitems
== 0)
4212 /* Empty line table. */
4216 /* Find the first linetable entry after the current function. Note that
4217 this also may be an end_sequence entry. */
4218 auto it
= std::lower_bound
4219 (linetable
->item
, linetable
->item
+ linetable
->nitems
, unrel_end
,
4220 [] (const linetable_entry
<e
, unrelocated_addr pc
)
4222 return lte
.unrelocated_pc () < pc
;
4224 if (it
== linetable
->item
+ linetable
->nitems
)
4226 /* We couldn't find either:
4227 - a linetable entry starting the function after the current
4229 - an end_sequence entry that terminates the current function
4232 This can happen when the linetable doesn't describe the full
4233 extent of the function. This can be triggered with:
4234 - compiler-generated debug info, in the cornercase that the pc
4235 with which we call find_pc_line resides in a different file
4237 - invalid dwarf assembly debug info.
4238 In the former case, there's no point in iterating further, simply
4239 return "not found". In the latter case, there's no current
4240 incentive to attempt to support this, so handle this
4241 conservatively and do the same. */
4245 if (unrel_end
< it
->unrelocated_pc ())
4247 /* We found a line entry that starts past the end of the
4248 function. This can happen if the previous entry straddles
4249 two functions, which shouldn't happen with compiler-generated
4250 debug info. Handle the corner case conservatively. */
4253 gdb_assert (unrel_end
== it
->unrelocated_pc ());
4255 /* Move to the last linetable entry of the current function. */
4256 if (it
== &linetable
->item
[0])
4258 /* Doing it-- would introduce undefined behavior, avoid it by
4259 explicitly handling this case. */
4263 if (it
->unrelocated_pc () < unrel_start
)
4265 /* Not in the current function. */
4268 gdb_assert (it
->unrelocated_pc () < unrel_end
);
4270 /* We're at the the last linetable entry of the current function. This
4271 is probably where the epilogue begins, but since the DWARF 5 spec
4272 doesn't guarantee it, we iterate backwards through the current
4273 function until we either find the epilogue beginning, or are sure
4274 that it doesn't exist. */
4275 for (; it
>= &linetable
->item
[0]; it
--)
4277 if (it
->unrelocated_pc () < unrel_start
)
4279 /* No longer in the current function. */
4283 if (it
->epilogue_begin
)
4285 /* Found the beginning of the epilogue. */
4286 return {it
->pc (objfile
)};
4289 if (it
== &linetable
->item
[0])
4291 /* No more entries in the current function.
4292 Doing it-- would introduce undefined behavior, avoid it by
4293 explicitly handling this case. */
4305 find_function_alias_target (bound_minimal_symbol msymbol
)
4307 CORE_ADDR func_addr
;
4308 if (!msymbol_is_function (msymbol
.objfile
, msymbol
.minsym
, &func_addr
))
4311 symbol
*sym
= find_pc_function (func_addr
);
4313 && sym
->aclass () == LOC_BLOCK
4314 && sym
->value_block ()->entry_pc () == func_addr
)
4321 /* If P is of the form "operator[ \t]+..." where `...' is
4322 some legitimate operator text, return a pointer to the
4323 beginning of the substring of the operator text.
4324 Otherwise, return "". */
4327 operator_chars (const char *p
, const char **end
)
4330 if (!startswith (p
, CP_OPERATOR_STR
))
4332 p
+= CP_OPERATOR_LEN
;
4334 /* Don't get faked out by `operator' being part of a longer
4336 if (isalpha (*p
) || *p
== '_' || *p
== '$' || *p
== '\0')
4339 /* Allow some whitespace between `operator' and the operator symbol. */
4340 while (*p
== ' ' || *p
== '\t')
4343 /* Recognize 'operator TYPENAME'. */
4345 if (isalpha (*p
) || *p
== '_' || *p
== '$')
4347 const char *q
= p
+ 1;
4349 while (isalnum (*q
) || *q
== '_' || *q
== '$')
4358 case '\\': /* regexp quoting */
4361 if (p
[2] == '=') /* 'operator\*=' */
4363 else /* 'operator\*' */
4367 else if (p
[1] == '[')
4370 error (_("mismatched quoting on brackets, "
4371 "try 'operator\\[\\]'"));
4372 else if (p
[2] == '\\' && p
[3] == ']')
4374 *end
= p
+ 4; /* 'operator\[\]' */
4378 error (_("nothing is allowed between '[' and ']'"));
4382 /* Gratuitous quote: skip it and move on. */
4404 if (p
[0] == '-' && p
[1] == '>')
4406 /* Struct pointer member operator 'operator->'. */
4409 *end
= p
+ 3; /* 'operator->*' */
4412 else if (p
[2] == '\\')
4414 *end
= p
+ 4; /* Hopefully 'operator->\*' */
4419 *end
= p
+ 2; /* 'operator->' */
4423 if (p
[1] == '=' || p
[1] == p
[0])
4434 error (_("`operator ()' must be specified "
4435 "without whitespace in `()'"));
4440 error (_("`operator ?:' must be specified "
4441 "without whitespace in `?:'"));
4446 error (_("`operator []' must be specified "
4447 "without whitespace in `[]'"));
4451 error (_("`operator %s' not supported"), p
);
4460 /* See class declaration. */
4462 info_sources_filter::info_sources_filter (match_on match_type
,
4464 : m_match_type (match_type
),
4467 /* Setup the compiled regular expression M_C_REGEXP based on M_REGEXP. */
4468 if (m_regexp
!= nullptr && *m_regexp
!= '\0')
4470 gdb_assert (m_regexp
!= nullptr);
4472 int cflags
= REG_NOSUB
;
4473 #ifdef HAVE_CASE_INSENSITIVE_FILE_SYSTEM
4474 cflags
|= REG_ICASE
;
4476 m_c_regexp
.emplace (m_regexp
, cflags
, _("Invalid regexp"));
4480 /* See class declaration. */
4483 info_sources_filter::matches (const char *fullname
) const
4485 /* Does it match regexp? */
4486 if (m_c_regexp
.has_value ())
4488 const char *to_match
;
4489 std::string dirname
;
4491 switch (m_match_type
)
4493 case match_on::DIRNAME
:
4494 dirname
= gdb_ldirname (fullname
);
4495 to_match
= dirname
.c_str ();
4497 case match_on::BASENAME
:
4498 to_match
= lbasename (fullname
);
4500 case match_on::FULLNAME
:
4501 to_match
= fullname
;
4504 gdb_assert_not_reached ("bad m_match_type");
4507 if (m_c_regexp
->exec (to_match
, 0, NULL
, 0) != 0)
4514 /* Data structure to maintain the state used for printing the results of
4515 the 'info sources' command. */
4517 struct output_source_filename_data
4519 /* Create an object for displaying the results of the 'info sources'
4520 command to UIOUT. FILTER must remain valid and unchanged for the
4521 lifetime of this object as this object retains a reference to FILTER. */
4522 output_source_filename_data (struct ui_out
*uiout
,
4523 const info_sources_filter
&filter
)
4524 : m_filter (filter
),
4528 DISABLE_COPY_AND_ASSIGN (output_source_filename_data
);
4530 /* Reset enough state of this object so we can match against a new set of
4531 files. The existing regular expression is retained though. */
4532 void reset_output ()
4535 m_filename_seen_cache
.clear ();
4538 /* Worker for sources_info, outputs the file name formatted for either
4539 cli or mi (based on the current_uiout). In cli mode displays
4540 FULLNAME with a comma separating this name from any previously
4541 printed name (line breaks are added at the comma). In MI mode
4542 outputs a tuple containing DISP_NAME (the files display name),
4543 FULLNAME, and EXPANDED_P (true when this file is from a fully
4544 expanded symtab, otherwise false). */
4545 void output (const char *disp_name
, const char *fullname
, bool expanded_p
);
4547 /* An overload suitable for use as a callback to
4548 quick_symbol_functions::map_symbol_filenames. */
4549 void operator() (const char *filename
, const char *fullname
)
4551 /* The false here indicates that this file is from an unexpanded
4553 output (filename
, fullname
, false);
4556 /* Return true if at least one filename has been printed (after a call to
4557 output) since either this object was created, or the last call to
4559 bool printed_filename_p () const
4566 /* Flag of whether we're printing the first one. */
4567 bool m_first
= true;
4569 /* Cache of what we've seen so far. */
4570 filename_seen_cache m_filename_seen_cache
;
4572 /* How source filename should be filtered. */
4573 const info_sources_filter
&m_filter
;
4575 /* The object to which output is sent. */
4576 struct ui_out
*m_uiout
;
4579 /* See comment in class declaration above. */
4582 output_source_filename_data::output (const char *disp_name
,
4583 const char *fullname
,
4586 /* Since a single source file can result in several partial symbol
4587 tables, we need to avoid printing it more than once. Note: if
4588 some of the psymtabs are read in and some are not, it gets
4589 printed both under "Source files for which symbols have been
4590 read" and "Source files for which symbols will be read in on
4591 demand". I consider this a reasonable way to deal with the
4592 situation. I'm not sure whether this can also happen for
4593 symtabs; it doesn't hurt to check. */
4595 /* Was NAME already seen? If so, then don't print it again. */
4596 if (m_filename_seen_cache
.seen (fullname
))
4599 /* If the filter rejects this file then don't print it. */
4600 if (!m_filter
.matches (fullname
))
4603 ui_out_emit_tuple
ui_emitter (m_uiout
, nullptr);
4605 /* Print it and reset *FIRST. */
4607 m_uiout
->text (", ");
4610 m_uiout
->wrap_hint (0);
4611 if (m_uiout
->is_mi_like_p ())
4613 m_uiout
->field_string ("file", disp_name
, file_name_style
.style ());
4614 if (fullname
!= nullptr)
4615 m_uiout
->field_string ("fullname", fullname
,
4616 file_name_style
.style ());
4617 m_uiout
->field_string ("debug-fully-read",
4618 (expanded_p
? "true" : "false"));
4622 if (fullname
== nullptr)
4623 fullname
= disp_name
;
4624 m_uiout
->field_string ("fullname", fullname
,
4625 file_name_style
.style ());
4629 /* For the 'info sources' command, what part of the file names should we be
4630 matching the user supplied regular expression against? */
4632 struct filename_partial_match_opts
4634 /* Only match the directory name part. */
4635 bool dirname
= false;
4637 /* Only match the basename part. */
4638 bool basename
= false;
4641 using isrc_flag_option_def
4642 = gdb::option::flag_option_def
<filename_partial_match_opts
>;
4644 static const gdb::option::option_def info_sources_option_defs
[] = {
4646 isrc_flag_option_def
{
4648 [] (filename_partial_match_opts
*opts
) { return &opts
->dirname
; },
4649 N_("Show only the files having a dirname matching REGEXP."),
4652 isrc_flag_option_def
{
4654 [] (filename_partial_match_opts
*opts
) { return &opts
->basename
; },
4655 N_("Show only the files having a basename matching REGEXP."),
4660 /* Create an option_def_group for the "info sources" options, with
4661 ISRC_OPTS as context. */
4663 static inline gdb::option::option_def_group
4664 make_info_sources_options_def_group (filename_partial_match_opts
*isrc_opts
)
4666 return {{info_sources_option_defs
}, isrc_opts
};
4669 /* Completer for "info sources". */
4672 info_sources_command_completer (cmd_list_element
*ignore
,
4673 completion_tracker
&tracker
,
4674 const char *text
, const char *word
)
4676 const auto group
= make_info_sources_options_def_group (nullptr);
4677 if (gdb::option::complete_options
4678 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
4685 info_sources_worker (struct ui_out
*uiout
,
4686 bool group_by_objfile
,
4687 const info_sources_filter
&filter
)
4689 output_source_filename_data
data (uiout
, filter
);
4691 ui_out_emit_list
results_emitter (uiout
, "files");
4692 std::optional
<ui_out_emit_tuple
> output_tuple
;
4693 std::optional
<ui_out_emit_list
> sources_list
;
4695 gdb_assert (group_by_objfile
|| uiout
->is_mi_like_p ());
4697 for (objfile
*objfile
: current_program_space
->objfiles ())
4699 if (group_by_objfile
)
4701 output_tuple
.emplace (uiout
, nullptr);
4702 uiout
->field_string ("filename", objfile_name (objfile
),
4703 file_name_style
.style ());
4704 uiout
->text (":\n");
4705 bool debug_fully_readin
= !objfile
->has_unexpanded_symtabs ();
4706 if (uiout
->is_mi_like_p ())
4708 const char *debug_info_state
;
4709 if (objfile
->has_symbols ())
4711 if (debug_fully_readin
)
4712 debug_info_state
= "fully-read";
4714 debug_info_state
= "partially-read";
4717 debug_info_state
= "none";
4718 current_uiout
->field_string ("debug-info", debug_info_state
);
4722 if (!debug_fully_readin
)
4723 uiout
->text ("(Full debug information has not yet been read "
4724 "for this file.)\n");
4725 if (!objfile
->has_symbols ())
4726 uiout
->text ("(Objfile has no debug information.)\n");
4729 sources_list
.emplace (uiout
, "sources");
4732 for (compunit_symtab
*cu
: objfile
->compunits ())
4734 for (symtab
*s
: cu
->filetabs ())
4736 const char *file
= symtab_to_filename_for_display (s
);
4737 const char *fullname
= symtab_to_fullname (s
);
4738 data
.output (file
, fullname
, true);
4742 if (group_by_objfile
)
4744 objfile
->map_symbol_filenames (data
, true /* need_fullname */);
4745 if (data
.printed_filename_p ())
4746 uiout
->text ("\n\n");
4747 data
.reset_output ();
4748 sources_list
.reset ();
4749 output_tuple
.reset ();
4753 if (!group_by_objfile
)
4755 data
.reset_output ();
4756 map_symbol_filenames (data
, true /*need_fullname*/);
4760 /* Implement the 'info sources' command. */
4763 info_sources_command (const char *args
, int from_tty
)
4765 if (!have_full_symbols (current_program_space
)
4766 && !have_partial_symbols (current_program_space
))
4767 error (_ ("No symbol table is loaded. Use the \"file\" command."));
4769 filename_partial_match_opts match_opts
;
4770 auto group
= make_info_sources_options_def_group (&match_opts
);
4771 gdb::option::process_options
4772 (&args
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, group
);
4774 if (match_opts
.dirname
&& match_opts
.basename
)
4775 error (_("You cannot give both -basename and -dirname to 'info sources'."));
4777 const char *regex
= nullptr;
4778 if (args
!= NULL
&& *args
!= '\000')
4781 if ((match_opts
.dirname
|| match_opts
.basename
) && regex
== nullptr)
4782 error (_("Missing REGEXP for 'info sources'."));
4784 info_sources_filter::match_on match_type
;
4785 if (match_opts
.dirname
)
4786 match_type
= info_sources_filter::match_on::DIRNAME
;
4787 else if (match_opts
.basename
)
4788 match_type
= info_sources_filter::match_on::BASENAME
;
4790 match_type
= info_sources_filter::match_on::FULLNAME
;
4792 info_sources_filter
filter (match_type
, regex
);
4793 info_sources_worker (current_uiout
, true, filter
);
4796 /* Compare FILE against all the entries of FILENAMES. If BASENAMES is
4797 true compare only lbasename of FILENAMES. */
4800 file_matches (const char *file
,
4801 const std::vector
<gdb::unique_xmalloc_ptr
<char>> &filenames
,
4804 if (filenames
.empty ())
4807 for (const auto &name
: filenames
)
4809 const char *lname
= (basenames
? lbasename (name
.get ()) : name
.get ());
4810 if (compare_filenames_for_search (file
, lname
))
4817 /* Helper function for std::sort on symbol_search objects. Can only sort
4818 symbols, not minimal symbols. */
4821 symbol_search::compare_search_syms (const symbol_search
&sym_a
,
4822 const symbol_search
&sym_b
)
4826 c
= FILENAME_CMP (sym_a
.symbol
->symtab ()->filename
,
4827 sym_b
.symbol
->symtab ()->filename
);
4831 if (sym_a
.block
!= sym_b
.block
)
4832 return sym_a
.block
- sym_b
.block
;
4834 return strcmp (sym_a
.symbol
->print_name (), sym_b
.symbol
->print_name ());
4837 /* Returns true if the type_name of symbol_type of SYM matches TREG.
4838 If SYM has no symbol_type or symbol_name, returns false. */
4841 treg_matches_sym_type_name (const compiled_regex
&treg
,
4842 const struct symbol
*sym
)
4844 struct type
*sym_type
;
4845 std::string printed_sym_type_name
;
4847 symbol_lookup_debug_printf_v ("treg_matches_sym_type_name, sym %s",
4848 sym
->natural_name ());
4850 sym_type
= sym
->type ();
4851 if (sym_type
== NULL
)
4855 scoped_switch_to_sym_language_if_auto
l (sym
);
4857 printed_sym_type_name
= type_to_string (sym_type
);
4860 symbol_lookup_debug_printf_v ("sym_type_name %s",
4861 printed_sym_type_name
.c_str ());
4863 if (printed_sym_type_name
.empty ())
4866 return treg
.exec (printed_sym_type_name
.c_str (), 0, NULL
, 0) == 0;
4872 global_symbol_searcher::is_suitable_msymbol
4873 (const domain_search_flags kind
, const minimal_symbol
*msymbol
)
4875 switch (msymbol
->type ())
4881 return (kind
& SEARCH_VAR_DOMAIN
) != 0;
4884 case mst_solib_trampoline
:
4885 case mst_text_gnu_ifunc
:
4886 return (kind
& SEARCH_FUNCTION_DOMAIN
) != 0;
4895 global_symbol_searcher::expand_symtabs
4896 (objfile
*objfile
, const std::optional
<compiled_regex
> &preg
) const
4898 domain_search_flags kind
= m_kind
;
4899 bool found_msymbol
= false;
4901 auto do_file_match
= [&] (const char *filename
, bool basenames
)
4903 return file_matches (filename
, m_filenames
, basenames
);
4905 expand_symtabs_file_matcher file_matcher
= nullptr;
4906 if (!m_filenames
.empty ())
4907 file_matcher
= do_file_match
;
4909 objfile
->expand_symtabs_matching
4911 &lookup_name_info::match_any (),
4912 [&] (const char *symname
)
4914 return (!preg
.has_value ()
4915 || preg
->exec (symname
, 0, NULL
, 0) == 0);
4918 SEARCH_GLOBAL_BLOCK
| SEARCH_STATIC_BLOCK
,
4921 /* Here, we search through the minimal symbol tables for functions and
4922 variables that match, and force their symbols to be read. This is in
4923 particular necessary for demangled variable names, which are no longer
4924 put into the partial symbol tables. The symbol will then be found
4925 during the scan of symtabs later.
4927 For functions, find_pc_symtab should succeed if we have debug info for
4928 the function, for variables we have to call
4929 lookup_symbol_in_objfile_from_linkage_name to determine if the
4930 variable has debug info. If the lookup fails, set found_msymbol so
4931 that we will rescan to print any matching symbols without debug info.
4932 We only search the objfile the msymbol came from, we no longer search
4933 all objfiles. In large programs (1000s of shared libs) searching all
4934 objfiles is not worth the pain. */
4935 if (m_filenames
.empty ()
4936 && (kind
& (SEARCH_VAR_DOMAIN
| SEARCH_FUNCTION_DOMAIN
)) != 0)
4938 for (minimal_symbol
*msymbol
: objfile
->msymbols ())
4942 if (msymbol
->created_by_gdb
)
4945 if (is_suitable_msymbol (kind
, msymbol
))
4947 if (!preg
.has_value ()
4948 || preg
->exec (msymbol
->natural_name (), 0,
4951 /* An important side-effect of these lookup functions is
4952 to expand the symbol table if msymbol is found, later
4953 in the process we will add matching symbols or
4954 msymbols to the results list, and that requires that
4955 the symbols tables are expanded. */
4956 if ((kind
& SEARCH_FUNCTION_DOMAIN
) != 0
4957 ? (find_pc_compunit_symtab
4958 (msymbol
->value_address (objfile
)) == NULL
)
4959 : (lookup_symbol_in_objfile_from_linkage_name
4960 (objfile
, msymbol
->linkage_name (),
4963 found_msymbol
= true;
4969 return found_msymbol
;
4975 global_symbol_searcher::add_matching_symbols
4977 const std::optional
<compiled_regex
> &preg
,
4978 const std::optional
<compiled_regex
> &treg
,
4979 std::set
<symbol_search
> *result_set
) const
4981 domain_search_flags kind
= m_kind
;
4983 /* Add matching symbols (if not already present). */
4984 for (compunit_symtab
*cust
: objfile
->compunits ())
4986 const struct blockvector
*bv
= cust
->blockvector ();
4988 for (block_enum block
: { GLOBAL_BLOCK
, STATIC_BLOCK
})
4990 const struct block
*b
= bv
->block (block
);
4992 for (struct symbol
*sym
: block_iterator_range (b
))
4994 struct symtab
*real_symtab
= sym
->symtab ();
4998 /* Check first sole REAL_SYMTAB->FILENAME. It does
4999 not need to be a substring of symtab_to_fullname as
5000 it may contain "./" etc. */
5001 if (!(file_matches (real_symtab
->filename
, m_filenames
, false)
5002 || ((basenames_may_differ
5003 || file_matches (lbasename (real_symtab
->filename
),
5005 && file_matches (symtab_to_fullname (real_symtab
),
5006 m_filenames
, false))))
5009 if (!sym
->matches (kind
))
5012 if (preg
.has_value () && preg
->exec (sym
->natural_name (), 0,
5016 if (((sym
->domain () == VAR_DOMAIN
5017 || sym
->domain () == FUNCTION_DOMAIN
)
5018 && treg
.has_value ()
5019 && !treg_matches_sym_type_name (*treg
, sym
)))
5022 if ((kind
& SEARCH_VAR_DOMAIN
) != 0)
5024 if (sym
->aclass () == LOC_UNRESOLVED
5025 /* LOC_CONST can be used for more than
5026 just enums, e.g., c++ static const
5027 members. We only want to skip enums
5029 || (sym
->aclass () == LOC_CONST
5030 && (sym
->type ()->code () == TYPE_CODE_ENUM
)))
5033 if (sym
->domain () == MODULE_DOMAIN
&& sym
->line () == 0)
5036 if (result_set
->size () < m_max_search_results
)
5038 /* Match, insert if not already in the results. */
5039 symbol_search
ss (block
, sym
);
5040 if (result_set
->find (ss
) == result_set
->end ())
5041 result_set
->insert (ss
);
5055 global_symbol_searcher::add_matching_msymbols
5056 (objfile
*objfile
, const std::optional
<compiled_regex
> &preg
,
5057 std::vector
<symbol_search
> *results
) const
5059 domain_search_flags kind
= m_kind
;
5061 for (minimal_symbol
*msymbol
: objfile
->msymbols ())
5065 if (msymbol
->created_by_gdb
)
5068 if (is_suitable_msymbol (kind
, msymbol
))
5070 if (!preg
.has_value ()
5071 || preg
->exec (msymbol
->natural_name (), 0,
5074 /* For functions we can do a quick check of whether the
5075 symbol might be found via find_pc_symtab. */
5076 if ((kind
& SEARCH_FUNCTION_DOMAIN
) == 0
5077 || (find_pc_compunit_symtab
5078 (msymbol
->value_address (objfile
)) == NULL
))
5080 if (lookup_symbol_in_objfile_from_linkage_name
5081 (objfile
, msymbol
->linkage_name (),
5082 SEARCH_VFT
).symbol
== NULL
)
5084 /* Matching msymbol, add it to the results list. */
5085 if (results
->size () < m_max_search_results
)
5086 results
->emplace_back (GLOBAL_BLOCK
, msymbol
, objfile
);
5100 std::vector
<symbol_search
>
5101 global_symbol_searcher::search () const
5103 std::optional
<compiled_regex
> preg
;
5104 std::optional
<compiled_regex
> treg
;
5106 if (m_symbol_name_regexp
!= NULL
)
5108 const char *symbol_name_regexp
= m_symbol_name_regexp
;
5109 std::string symbol_name_regexp_holder
;
5111 /* Make sure spacing is right for C++ operators.
5112 This is just a courtesy to make the matching less sensitive
5113 to how many spaces the user leaves between 'operator'
5114 and <TYPENAME> or <OPERATOR>. */
5116 const char *opname
= operator_chars (symbol_name_regexp
, &opend
);
5120 int fix
= -1; /* -1 means ok; otherwise number of
5123 if (isalpha (*opname
) || *opname
== '_' || *opname
== '$')
5125 /* There should 1 space between 'operator' and 'TYPENAME'. */
5126 if (opname
[-1] != ' ' || opname
[-2] == ' ')
5131 /* There should 0 spaces between 'operator' and 'OPERATOR'. */
5132 if (opname
[-1] == ' ')
5135 /* If wrong number of spaces, fix it. */
5138 symbol_name_regexp_holder
5139 = string_printf ("operator%.*s%s", fix
, " ", opname
);
5140 symbol_name_regexp
= symbol_name_regexp_holder
.c_str ();
5144 int cflags
= REG_NOSUB
| (case_sensitivity
== case_sensitive_off
5146 preg
.emplace (symbol_name_regexp
, cflags
,
5147 _("Invalid regexp"));
5150 if (m_symbol_type_regexp
!= NULL
)
5152 int cflags
= REG_NOSUB
| (case_sensitivity
== case_sensitive_off
5154 treg
.emplace (m_symbol_type_regexp
, cflags
,
5155 _("Invalid regexp"));
5158 bool found_msymbol
= false;
5159 std::set
<symbol_search
> result_set
;
5160 for (objfile
*objfile
: current_program_space
->objfiles ())
5162 /* Expand symtabs within objfile that possibly contain matching
5164 found_msymbol
|= expand_symtabs (objfile
, preg
);
5166 /* Find matching symbols within OBJFILE and add them in to the
5167 RESULT_SET set. Use a set here so that we can easily detect
5168 duplicates as we go, and can therefore track how many unique
5169 matches we have found so far. */
5170 if (!add_matching_symbols (objfile
, preg
, treg
, &result_set
))
5174 /* Convert the result set into a sorted result list, as std::set is
5175 defined to be sorted then no explicit call to std::sort is needed. */
5176 std::vector
<symbol_search
> result (result_set
.begin (), result_set
.end ());
5178 /* If there are no debug symbols, then add matching minsyms. But if the
5179 user wants to see symbols matching a type regexp, then never give a
5180 minimal symbol, as we assume that a minimal symbol does not have a
5183 || (m_filenames
.empty () && (m_kind
& SEARCH_VAR_DOMAIN
) != 0))
5184 && !m_exclude_minsyms
5185 && !treg
.has_value ())
5187 gdb_assert ((m_kind
& (SEARCH_VAR_DOMAIN
| SEARCH_FUNCTION_DOMAIN
))
5189 for (objfile
*objfile
: current_program_space
->objfiles ())
5190 if (!add_matching_msymbols (objfile
, preg
, &result
))
5200 symbol_to_info_string (struct symbol
*sym
, int block
)
5204 gdb_assert (block
== GLOBAL_BLOCK
|| block
== STATIC_BLOCK
);
5206 if (block
== STATIC_BLOCK
5207 && (sym
->domain () == VAR_DOMAIN
5208 || sym
->domain () == FUNCTION_DOMAIN
))
5211 /* Typedef that is not a C++ class. */
5212 if (sym
->domain () == TYPE_DOMAIN
)
5214 string_file tmp_stream
;
5216 /* FIXME: For C (and C++) we end up with a difference in output here
5217 between how a typedef is printed, and non-typedefs are printed.
5218 The TYPEDEF_PRINT code places a ";" at the end in an attempt to
5219 appear C-like, while TYPE_PRINT doesn't.
5221 For the struct printing case below, things are worse, we force
5222 printing of the ";" in this function, which is going to be wrong
5223 for languages that don't require a ";" between statements. */
5224 if (sym
->type ()->code () == TYPE_CODE_TYPEDEF
)
5225 typedef_print (sym
->type (), sym
, &tmp_stream
);
5227 type_print (sym
->type (), "", &tmp_stream
, -1);
5228 str
+= tmp_stream
.string ();
5230 /* variable, func, or typedef-that-is-c++-class. */
5231 else if (sym
->domain () == VAR_DOMAIN
|| sym
->domain () == STRUCT_DOMAIN
5232 || sym
->domain () == FUNCTION_DOMAIN
)
5234 string_file tmp_stream
;
5236 type_print (sym
->type (),
5237 (sym
->aclass () == LOC_TYPEDEF
5238 ? "" : sym
->print_name ()),
5241 str
+= tmp_stream
.string ();
5244 /* Printing of modules is currently done here, maybe at some future
5245 point we might want a language specific method to print the module
5246 symbol so that we can customise the output more. */
5247 else if (sym
->domain () == MODULE_DOMAIN
)
5248 str
+= sym
->print_name ();
5253 /* Helper function for symbol info commands, for example 'info
5254 functions', 'info variables', etc. BLOCK is the type of block the
5255 symbols was found in, either GLOBAL_BLOCK or STATIC_BLOCK. SYM is
5256 the symbol we found. If LAST is not NULL, print file and line
5257 number information for the symbol as well. Skip printing the
5258 filename if it matches LAST. */
5261 print_symbol_info (struct symbol
*sym
, int block
, const char *last
)
5263 scoped_switch_to_sym_language_if_auto
l (sym
);
5264 struct symtab
*s
= sym
->symtab ();
5268 const char *s_filename
= symtab_to_filename_for_display (s
);
5270 if (filename_cmp (last
, s_filename
) != 0)
5272 gdb_printf (_("\nFile %ps:\n"),
5273 styled_string (file_name_style
.style (),
5277 if (sym
->line () != 0)
5278 gdb_printf ("%d:\t", sym
->line ());
5283 std::string str
= symbol_to_info_string (sym
, block
);
5284 gdb_printf ("%s\n", str
.c_str ());
5287 /* This help function for symtab_symbol_info() prints information
5288 for non-debugging symbols to gdb_stdout. */
5291 print_msymbol_info (bound_minimal_symbol msymbol
)
5293 struct gdbarch
*gdbarch
= msymbol
.objfile
->arch ();
5296 if (gdbarch_addr_bit (gdbarch
) <= 32)
5297 tmp
= hex_string_custom (msymbol
.value_address ()
5298 & (CORE_ADDR
) 0xffffffff,
5301 tmp
= hex_string_custom (msymbol
.value_address (),
5304 ui_file_style sym_style
= (msymbol
.minsym
->text_p ()
5305 ? function_name_style
.style ()
5306 : ui_file_style ());
5308 gdb_printf (_("%ps %ps\n"),
5309 styled_string (address_style
.style (), tmp
),
5310 styled_string (sym_style
, msymbol
.minsym
->print_name ()));
5313 /* This is the guts of the commands "info functions", "info types", and
5314 "info variables". It calls search_symbols to find all matches and then
5315 print_[m]symbol_info to print out some useful information about the
5319 symtab_symbol_info (bool quiet
, bool exclude_minsyms
,
5320 const char *regexp
, domain_enum kind
,
5321 const char *t_regexp
, int from_tty
)
5323 const char *last_filename
= "";
5326 if (regexp
!= nullptr && *regexp
== '\0')
5329 domain_search_flags flags
= to_search_flags (kind
);
5330 if (kind
== TYPE_DOMAIN
)
5331 flags
|= SEARCH_STRUCT_DOMAIN
;
5333 global_symbol_searcher
spec (flags
, regexp
);
5334 spec
.set_symbol_type_regexp (t_regexp
);
5335 spec
.set_exclude_minsyms (exclude_minsyms
);
5336 std::vector
<symbol_search
> symbols
= spec
.search ();
5340 const char *classname
;
5344 classname
= "variable";
5346 case FUNCTION_DOMAIN
:
5347 classname
= "function";
5353 classname
= "module";
5356 gdb_assert_not_reached ("invalid domain enum");
5361 if (t_regexp
!= NULL
)
5363 (_("All %ss matching regular expression \"%s\""
5364 " with type matching regular expression \"%s\":\n"),
5365 classname
, regexp
, t_regexp
);
5367 gdb_printf (_("All %ss matching regular expression \"%s\":\n"),
5372 if (t_regexp
!= NULL
)
5374 (_("All defined %ss"
5375 " with type matching regular expression \"%s\" :\n"),
5376 classname
, t_regexp
);
5378 gdb_printf (_("All defined %ss:\n"), classname
);
5382 for (const symbol_search
&p
: symbols
)
5386 if (p
.msymbol
.minsym
!= NULL
)
5391 gdb_printf (_("\nNon-debugging symbols:\n"));
5394 print_msymbol_info (p
.msymbol
);
5398 print_symbol_info (p
.symbol
, p
.block
, last_filename
);
5400 = symtab_to_filename_for_display (p
.symbol
->symtab ());
5405 /* Structure to hold the values of the options used by the 'info variables'
5406 and 'info functions' commands. These correspond to the -q, -t, and -n
5409 struct info_vars_funcs_options
5412 bool exclude_minsyms
= false;
5413 std::string type_regexp
;
5416 /* The options used by the 'info variables' and 'info functions'
5419 static const gdb::option::option_def info_vars_funcs_options_defs
[] = {
5420 gdb::option::boolean_option_def
<info_vars_funcs_options
> {
5422 [] (info_vars_funcs_options
*opt
) { return &opt
->quiet
; },
5423 nullptr, /* show_cmd_cb */
5424 nullptr /* set_doc */
5427 gdb::option::boolean_option_def
<info_vars_funcs_options
> {
5429 [] (info_vars_funcs_options
*opt
) { return &opt
->exclude_minsyms
; },
5430 nullptr, /* show_cmd_cb */
5431 nullptr /* set_doc */
5434 gdb::option::string_option_def
<info_vars_funcs_options
> {
5436 [] (info_vars_funcs_options
*opt
) { return &opt
->type_regexp
; },
5437 nullptr, /* show_cmd_cb */
5438 nullptr /* set_doc */
5442 /* Returns the option group used by 'info variables' and 'info
5445 static gdb::option::option_def_group
5446 make_info_vars_funcs_options_def_group (info_vars_funcs_options
*opts
)
5448 return {{info_vars_funcs_options_defs
}, opts
};
5451 /* Command completer for 'info variables' and 'info functions'. */
5454 info_vars_funcs_command_completer (struct cmd_list_element
*ignore
,
5455 completion_tracker
&tracker
,
5456 const char *text
, const char * /* word */)
5459 = make_info_vars_funcs_options_def_group (nullptr);
5460 if (gdb::option::complete_options
5461 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
5464 const char *word
= advance_to_expression_complete_word_point (tracker
, text
);
5465 symbol_completer (ignore
, tracker
, text
, word
);
5468 /* Implement the 'info variables' command. */
5471 info_variables_command (const char *args
, int from_tty
)
5473 info_vars_funcs_options opts
;
5474 auto grp
= make_info_vars_funcs_options_def_group (&opts
);
5475 gdb::option::process_options
5476 (&args
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, grp
);
5477 if (args
!= nullptr && *args
== '\0')
5481 (opts
.quiet
, opts
.exclude_minsyms
, args
, VAR_DOMAIN
,
5482 opts
.type_regexp
.empty () ? nullptr : opts
.type_regexp
.c_str (),
5486 /* Implement the 'info functions' command. */
5489 info_functions_command (const char *args
, int from_tty
)
5491 info_vars_funcs_options opts
;
5493 auto grp
= make_info_vars_funcs_options_def_group (&opts
);
5494 gdb::option::process_options
5495 (&args
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, grp
);
5496 if (args
!= nullptr && *args
== '\0')
5500 (opts
.quiet
, opts
.exclude_minsyms
, args
, FUNCTION_DOMAIN
,
5501 opts
.type_regexp
.empty () ? nullptr : opts
.type_regexp
.c_str (),
5505 /* Holds the -q option for the 'info types' command. */
5507 struct info_types_options
5512 /* The options used by the 'info types' command. */
5514 static const gdb::option::option_def info_types_options_defs
[] = {
5515 gdb::option::boolean_option_def
<info_types_options
> {
5517 [] (info_types_options
*opt
) { return &opt
->quiet
; },
5518 nullptr, /* show_cmd_cb */
5519 nullptr /* set_doc */
5523 /* Returns the option group used by 'info types'. */
5525 static gdb::option::option_def_group
5526 make_info_types_options_def_group (info_types_options
*opts
)
5528 return {{info_types_options_defs
}, opts
};
5531 /* Implement the 'info types' command. */
5534 info_types_command (const char *args
, int from_tty
)
5536 info_types_options opts
;
5538 auto grp
= make_info_types_options_def_group (&opts
);
5539 gdb::option::process_options
5540 (&args
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, grp
);
5541 if (args
!= nullptr && *args
== '\0')
5543 symtab_symbol_info (opts
.quiet
, false, args
, TYPE_DOMAIN
, nullptr,
5547 /* Command completer for 'info types' command. */
5550 info_types_command_completer (struct cmd_list_element
*ignore
,
5551 completion_tracker
&tracker
,
5552 const char *text
, const char * /* word */)
5555 = make_info_types_options_def_group (nullptr);
5556 if (gdb::option::complete_options
5557 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
5560 const char *word
= advance_to_expression_complete_word_point (tracker
, text
);
5561 symbol_completer (ignore
, tracker
, text
, word
);
5564 /* Implement the 'info modules' command. */
5567 info_modules_command (const char *args
, int from_tty
)
5569 info_types_options opts
;
5571 auto grp
= make_info_types_options_def_group (&opts
);
5572 gdb::option::process_options
5573 (&args
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, grp
);
5574 if (args
!= nullptr && *args
== '\0')
5576 symtab_symbol_info (opts
.quiet
, true, args
, MODULE_DOMAIN
, nullptr,
5580 /* Implement the 'info main' command. */
5583 info_main_command (const char *args
, int from_tty
)
5585 gdb_printf ("%s\n", main_name ());
5589 rbreak_command (const char *regexp
, int from_tty
)
5591 gdb::unique_xmalloc_ptr
<char> file_name
;
5593 if (regexp
!= nullptr)
5595 const char *colon
= strchr (regexp
, ':');
5597 /* Ignore the colon if it is part of a Windows drive. */
5598 if (HAS_DRIVE_SPEC (regexp
)
5599 && (regexp
[2] == '/' || regexp
[2] == '\\'))
5600 colon
= strchr (STRIP_DRIVE_SPEC (regexp
), ':');
5602 if (colon
&& *(colon
+ 1) != ':')
5604 int colon_index
= colon
- regexp
;
5605 while (colon_index
> 0 && isspace (regexp
[colon_index
- 1]))
5608 file_name
= make_unique_xstrndup (regexp
, colon_index
);
5609 regexp
= skip_spaces (colon
+ 1);
5613 global_symbol_searcher
spec (SEARCH_FUNCTION_DOMAIN
, regexp
);
5614 if (file_name
!= nullptr)
5615 spec
.add_filename (std::move (file_name
));
5616 std::vector
<symbol_search
> symbols
= spec
.search ();
5618 gdb::unordered_set
<std::string
> seen_names
;
5619 scoped_rbreak_breakpoints finalize
;
5622 for (const symbol_search
&p
: symbols
)
5625 if (p
.msymbol
.minsym
== nullptr)
5627 if (file_name
!= nullptr)
5629 struct symtab
*symtab
= p
.symbol
->symtab ();
5630 const char *fullname
= symtab_to_fullname (symtab
);
5631 name
= string_printf ("%s:'%s'", fullname
,
5632 p
.symbol
->linkage_name ());
5635 name
= p
.symbol
->linkage_name ();
5638 name
= p
.msymbol
.minsym
->linkage_name ();
5640 if (!seen_names
.insert (name
).second
)
5645 break_command (name
.c_str (), from_tty
);
5647 catch (const gdb_exception_error
&ex
)
5649 exception_print (gdb_stderr
, ex
);
5654 if (p
.msymbol
.minsym
== nullptr)
5655 print_symbol_info (p
.symbol
, p
.block
, nullptr);
5657 gdb_printf ("<function, no debug info> %s;\n", name
.c_str ());
5660 int first_bp
= finalize
.first_breakpoint ();
5661 int last_bp
= finalize
.last_breakpoint ();
5664 gdb_printf (_("No breakpoints made.\n"));
5665 else if (first_bp
== last_bp
)
5666 gdb_printf (_("Successfully created breakpoint %d.\n"), first_bp
);
5668 gdb_printf (_("Successfully created breakpoints %d-%d.\n"),
5672 gdb_printf (_("%d breakpoints failed due to errors, see above.\n"),
5677 /* Evaluate if SYMNAME matches LOOKUP_NAME. */
5680 compare_symbol_name (const char *symbol_name
, language symbol_language
,
5681 const lookup_name_info
&lookup_name
,
5682 completion_match_result
&match_res
)
5684 const language_defn
*lang
= language_def (symbol_language
);
5686 symbol_name_matcher_ftype
*name_match
5687 = lang
->get_symbol_name_matcher (lookup_name
);
5689 return name_match (symbol_name
, lookup_name
, &match_res
);
5695 completion_list_add_name (completion_tracker
&tracker
,
5696 language symbol_language
,
5697 const char *symname
,
5698 const lookup_name_info
&lookup_name
,
5699 const char *text
, const char *word
)
5701 completion_match_result
&match_res
5702 = tracker
.reset_completion_match_result ();
5704 /* Clip symbols that cannot match. */
5705 if (!compare_symbol_name (symname
, symbol_language
, lookup_name
, match_res
))
5708 /* Refresh SYMNAME from the match string. It's potentially
5709 different depending on language. (E.g., on Ada, the match may be
5710 the encoded symbol name wrapped in "<>"). */
5711 symname
= match_res
.match
.match ();
5712 gdb_assert (symname
!= NULL
);
5714 /* We have a match for a completion, so add SYMNAME to the current list
5715 of matches. Note that the name is moved to freshly malloc'd space. */
5718 gdb::unique_xmalloc_ptr
<char> completion
5719 = make_completion_match_str (symname
, text
, word
);
5721 /* Here we pass the match-for-lcd object to add_completion. Some
5722 languages match the user text against substrings of symbol
5723 names in some cases. E.g., in C++, "b push_ba" completes to
5724 "std::vector::push_back", "std::string::push_back", etc., and
5725 in this case we want the completion lowest common denominator
5726 to be "push_back" instead of "std::". */
5727 tracker
.add_completion (std::move (completion
),
5728 &match_res
.match_for_lcd
, text
, word
);
5734 /* completion_list_add_name wrapper for struct symbol. */
5737 completion_list_add_symbol (completion_tracker
&tracker
,
5739 const lookup_name_info
&lookup_name
,
5740 const char *text
, const char *word
)
5742 if (!completion_list_add_name (tracker
, sym
->language (),
5743 sym
->natural_name (),
5744 lookup_name
, text
, word
))
5747 /* C++ function symbols include the parameters within both the msymbol
5748 name and the symbol name. The problem is that the msymbol name will
5749 describe the parameters in the most basic way, with typedefs stripped
5750 out, while the symbol name will represent the types as they appear in
5751 the program. This means we will see duplicate entries in the
5752 completion tracker. The following converts the symbol name back to
5753 the msymbol name and removes the msymbol name from the completion
5755 if (sym
->language () == language_cplus
5756 && sym
->aclass () == LOC_BLOCK
)
5758 /* The call to canonicalize returns the empty string if the input
5759 string is already in canonical form, thanks to this we don't
5760 remove the symbol we just added above. */
5761 gdb::unique_xmalloc_ptr
<char> str
5762 = cp_canonicalize_string_no_typedefs (sym
->natural_name ());
5764 tracker
.remove_completion (str
.get ());
5768 /* completion_list_add_name wrapper for struct minimal_symbol. */
5771 completion_list_add_msymbol (completion_tracker
&tracker
,
5772 minimal_symbol
*sym
,
5773 const lookup_name_info
&lookup_name
,
5774 const char *text
, const char *word
)
5776 completion_list_add_name (tracker
, sym
->language (),
5777 sym
->natural_name (),
5778 lookup_name
, text
, word
);
5782 /* ObjC: In case we are completing on a selector, look as the msymbol
5783 again and feed all the selectors into the mill. */
5786 completion_list_objc_symbol (completion_tracker
&tracker
,
5787 struct minimal_symbol
*msymbol
,
5788 const lookup_name_info
&lookup_name
,
5789 const char *text
, const char *word
)
5791 static char *tmp
= NULL
;
5792 static unsigned int tmplen
= 0;
5794 const char *method
, *category
, *selector
;
5797 method
= msymbol
->natural_name ();
5799 /* Is it a method? */
5800 if ((method
[0] != '-') && (method
[0] != '+'))
5804 /* Complete on shortened method method. */
5805 completion_list_add_name (tracker
, language_objc
,
5810 while ((strlen (method
) + 1) >= tmplen
)
5816 tmp
= (char *) xrealloc (tmp
, tmplen
);
5818 selector
= strchr (method
, ' ');
5819 if (selector
!= NULL
)
5822 category
= strchr (method
, '(');
5824 if ((category
!= NULL
) && (selector
!= NULL
))
5826 memcpy (tmp
, method
, (category
- method
));
5827 tmp
[category
- method
] = ' ';
5828 memcpy (tmp
+ (category
- method
) + 1, selector
, strlen (selector
) + 1);
5829 completion_list_add_name (tracker
, language_objc
, tmp
,
5830 lookup_name
, text
, word
);
5832 completion_list_add_name (tracker
, language_objc
, tmp
+ 1,
5833 lookup_name
, text
, word
);
5836 if (selector
!= NULL
)
5838 /* Complete on selector only. */
5839 strcpy (tmp
, selector
);
5840 tmp2
= strchr (tmp
, ']');
5844 completion_list_add_name (tracker
, language_objc
, tmp
,
5845 lookup_name
, text
, word
);
5849 /* Break the non-quoted text based on the characters which are in
5850 symbols. FIXME: This should probably be language-specific. */
5853 language_search_unquoted_string (const char *text
, const char *p
)
5855 for (; p
> text
; --p
)
5857 if (isalnum (p
[-1]) || p
[-1] == '_' || p
[-1] == '\0')
5861 if ((current_language
->la_language
== language_objc
))
5863 if (p
[-1] == ':') /* Might be part of a method name. */
5865 else if (p
[-1] == '[' && (p
[-2] == '-' || p
[-2] == '+'))
5866 p
-= 2; /* Beginning of a method name. */
5867 else if (p
[-1] == ' ' || p
[-1] == '(' || p
[-1] == ')')
5868 { /* Might be part of a method name. */
5871 /* Seeing a ' ' or a '(' is not conclusive evidence
5872 that we are in the middle of a method name. However,
5873 finding "-[" or "+[" should be pretty un-ambiguous.
5874 Unfortunately we have to find it now to decide. */
5877 if (isalnum (t
[-1]) || t
[-1] == '_' ||
5878 t
[-1] == ' ' || t
[-1] == ':' ||
5879 t
[-1] == '(' || t
[-1] == ')')
5884 if (t
[-1] == '[' && (t
[-2] == '-' || t
[-2] == '+'))
5885 p
= t
- 2; /* Method name detected. */
5886 /* Else we leave with p unchanged. */
5896 completion_list_add_fields (completion_tracker
&tracker
,
5898 const lookup_name_info
&lookup_name
,
5899 const char *text
, const char *word
)
5901 if (sym
->aclass () == LOC_TYPEDEF
)
5903 struct type
*t
= sym
->type ();
5904 enum type_code c
= t
->code ();
5907 if (c
== TYPE_CODE_UNION
|| c
== TYPE_CODE_STRUCT
)
5908 for (j
= TYPE_N_BASECLASSES (t
); j
< t
->num_fields (); j
++)
5909 if (t
->field (j
).name ())
5910 completion_list_add_name (tracker
, sym
->language (),
5911 t
->field (j
).name (),
5912 lookup_name
, text
, word
);
5919 symbol_is_function_or_method (symbol
*sym
)
5921 switch (sym
->type ()->code ())
5923 case TYPE_CODE_FUNC
:
5924 case TYPE_CODE_METHOD
:
5934 symbol_is_function_or_method (minimal_symbol
*msymbol
)
5936 switch (msymbol
->type ())
5939 case mst_text_gnu_ifunc
:
5940 case mst_solib_trampoline
:
5950 bound_minimal_symbol
5951 find_gnu_ifunc (const symbol
*sym
)
5953 if (sym
->aclass () != LOC_BLOCK
)
5956 lookup_name_info
lookup_name (sym
->search_name (),
5957 symbol_name_match_type::SEARCH_NAME
);
5958 struct objfile
*objfile
= sym
->objfile ();
5960 CORE_ADDR address
= sym
->value_block ()->entry_pc ();
5961 minimal_symbol
*ifunc
= NULL
;
5963 iterate_over_minimal_symbols (objfile
, lookup_name
,
5964 [&] (minimal_symbol
*minsym
)
5966 if (minsym
->type () == mst_text_gnu_ifunc
5967 || minsym
->type () == mst_data_gnu_ifunc
)
5969 CORE_ADDR msym_addr
= minsym
->value_address (objfile
);
5970 if (minsym
->type () == mst_data_gnu_ifunc
)
5972 struct gdbarch
*gdbarch
= objfile
->arch ();
5973 msym_addr
= gdbarch_convert_from_func_ptr_addr
5974 (gdbarch
, msym_addr
, current_inferior ()->top_target ());
5976 if (msym_addr
== address
)
5986 return {ifunc
, objfile
};
5990 /* Add matching symbols from SYMTAB to the current completion list. */
5993 add_symtab_completions (struct compunit_symtab
*cust
,
5994 completion_tracker
&tracker
,
5995 complete_symbol_mode mode
,
5996 const lookup_name_info
&lookup_name
,
5997 const char *text
, const char *word
,
5998 enum type_code code
)
6005 for (i
= GLOBAL_BLOCK
; i
<= STATIC_BLOCK
; i
++)
6009 const struct block
*b
= cust
->blockvector ()->block (i
);
6010 for (struct symbol
*sym
: block_iterator_range (b
))
6012 if (completion_skip_symbol (mode
, sym
))
6015 if (code
== TYPE_CODE_UNDEF
6016 || (sym
->domain () == STRUCT_DOMAIN
6017 && sym
->type ()->code () == code
))
6018 completion_list_add_symbol (tracker
, sym
,
6026 default_collect_symbol_completion_matches_break_on
6027 (completion_tracker
&tracker
, complete_symbol_mode mode
,
6028 symbol_name_match_type name_match_type
,
6029 const char *text
, const char *word
,
6030 const char *break_on
, enum type_code code
)
6032 /* Problem: All of the symbols have to be copied because readline
6033 frees them. I'm not going to worry about this; hopefully there
6034 won't be that many. */
6036 const struct block
*b
;
6037 const struct block
*surrounding_static_block
, *surrounding_global_block
;
6038 /* The symbol we are completing on. Points in same buffer as text. */
6039 const char *sym_text
;
6041 /* Now look for the symbol we are supposed to complete on. */
6042 if (mode
== complete_symbol_mode::LINESPEC
)
6048 const char *quote_pos
= NULL
;
6050 /* First see if this is a quoted string. */
6052 for (p
= text
; *p
!= '\0'; ++p
)
6054 if (quote_found
!= '\0')
6056 if (*p
== quote_found
)
6057 /* Found close quote. */
6059 else if (*p
== '\\' && p
[1] == quote_found
)
6060 /* A backslash followed by the quote character
6061 doesn't end the string. */
6064 else if (*p
== '\'' || *p
== '"')
6070 if (quote_found
== '\'')
6071 /* A string within single quotes can be a symbol, so complete on it. */
6072 sym_text
= quote_pos
+ 1;
6073 else if (quote_found
== '"')
6074 /* A double-quoted string is never a symbol, nor does it make sense
6075 to complete it any other way. */
6081 /* It is not a quoted string. Break it based on the characters
6082 which are in symbols. */
6085 if (isalnum (p
[-1]) || p
[-1] == '_' || p
[-1] == '\0'
6086 || p
[-1] == ':' || strchr (break_on
, p
[-1]) != NULL
)
6095 lookup_name_info
lookup_name (sym_text
, name_match_type
, true);
6097 /* At this point scan through the misc symbol vectors and add each
6098 symbol you find to the list. Eventually we want to ignore
6099 anything that isn't a text symbol (everything else will be
6100 handled by the psymtab code below). */
6102 if (code
== TYPE_CODE_UNDEF
)
6104 for (objfile
*objfile
: current_program_space
->objfiles ())
6106 for (minimal_symbol
*msymbol
: objfile
->msymbols ())
6110 if (completion_skip_symbol (mode
, msymbol
))
6113 completion_list_add_msymbol (tracker
, msymbol
, lookup_name
,
6116 completion_list_objc_symbol (tracker
, msymbol
, lookup_name
,
6122 /* Add completions for all currently loaded symbol tables. */
6123 for (objfile
*objfile
: current_program_space
->objfiles ())
6125 for (compunit_symtab
*cust
: objfile
->compunits ())
6126 add_symtab_completions (cust
, tracker
, mode
, lookup_name
,
6127 sym_text
, word
, code
);
6130 /* Look through the partial symtabs for all symbols which begin by
6131 matching SYM_TEXT. Expand all CUs that you find to the list. */
6132 expand_symtabs_matching (NULL
,
6135 [&] (compunit_symtab
*symtab
) /* expansion notify */
6137 add_symtab_completions (symtab
,
6138 tracker
, mode
, lookup_name
,
6139 sym_text
, word
, code
);
6142 SEARCH_GLOBAL_BLOCK
| SEARCH_STATIC_BLOCK
,
6143 SEARCH_ALL_DOMAINS
);
6145 /* Search upwards from currently selected frame (so that we can
6146 complete on local vars). Also catch fields of types defined in
6147 this places which match our text string. Only complete on types
6148 visible from current context. */
6150 b
= get_selected_block (0);
6151 surrounding_static_block
= b
== nullptr ? nullptr : b
->static_block ();
6152 surrounding_global_block
= b
== nullptr ? nullptr : b
->global_block ();
6153 if (surrounding_static_block
!= NULL
)
6154 while (b
!= surrounding_static_block
)
6158 for (struct symbol
*sym
: block_iterator_range (b
))
6160 if (code
== TYPE_CODE_UNDEF
)
6162 completion_list_add_symbol (tracker
, sym
, lookup_name
,
6164 completion_list_add_fields (tracker
, sym
, lookup_name
,
6167 else if (sym
->domain () == STRUCT_DOMAIN
6168 && sym
->type ()->code () == code
)
6169 completion_list_add_symbol (tracker
, sym
, lookup_name
,
6173 /* Stop when we encounter an enclosing function. Do not stop for
6174 non-inlined functions - the locals of the enclosing function
6175 are in scope for a nested function. */
6176 if (b
->function () != NULL
&& b
->inlined_p ())
6178 b
= b
->superblock ();
6181 /* Add fields from the file's types; symbols will be added below. */
6183 if (code
== TYPE_CODE_UNDEF
)
6185 if (surrounding_static_block
!= NULL
)
6186 for (struct symbol
*sym
: block_iterator_range (surrounding_static_block
))
6187 completion_list_add_fields (tracker
, sym
, lookup_name
,
6190 if (surrounding_global_block
!= NULL
)
6191 for (struct symbol
*sym
: block_iterator_range (surrounding_global_block
))
6192 completion_list_add_fields (tracker
, sym
, lookup_name
,
6196 /* Skip macros if we are completing a struct tag -- arguable but
6197 usually what is expected. */
6198 if (current_language
->macro_expansion () == macro_expansion_c
6199 && code
== TYPE_CODE_UNDEF
)
6201 /* This adds a macro's name to the current completion list. */
6202 auto add_macro_name
= [&] (const char *macro_name
,
6203 const macro_definition
*,
6204 macro_source_file
*,
6207 completion_list_add_name (tracker
, language_c
, macro_name
,
6208 lookup_name
, sym_text
, word
);
6211 /* Add any macros visible in the default scope. Note that this
6212 may yield the occasional wrong result, because an expression
6213 might be evaluated in a scope other than the default. For
6214 example, if the user types "break file:line if <TAB>", the
6215 resulting expression will be evaluated at "file:line" -- but
6216 at there does not seem to be a way to detect this at
6218 macro_scope scope
= default_macro_scope ();
6219 if (scope
.is_valid ())
6220 macro_for_each_in_scope (scope
.file
, scope
.line
, add_macro_name
);
6222 /* User-defined macros are always visible. */
6223 macro_for_each (macro_user_macros
, add_macro_name
);
6227 /* Collect all symbols (regardless of class) which begin by matching
6231 collect_symbol_completion_matches (completion_tracker
&tracker
,
6232 complete_symbol_mode mode
,
6233 symbol_name_match_type name_match_type
,
6234 const char *text
, const char *word
)
6236 current_language
->collect_symbol_completion_matches (tracker
, mode
,
6242 /* Like collect_symbol_completion_matches, but only collect
6243 STRUCT_DOMAIN symbols whose type code is CODE. */
6246 collect_symbol_completion_matches_type (completion_tracker
&tracker
,
6247 const char *text
, const char *word
,
6248 enum type_code code
)
6250 complete_symbol_mode mode
= complete_symbol_mode::EXPRESSION
;
6251 symbol_name_match_type name_match_type
= symbol_name_match_type::EXPRESSION
;
6253 gdb_assert (code
== TYPE_CODE_UNION
6254 || code
== TYPE_CODE_STRUCT
6255 || code
== TYPE_CODE_ENUM
);
6256 current_language
->collect_symbol_completion_matches (tracker
, mode
,
6261 /* Like collect_symbol_completion_matches, but collects a list of
6262 symbols defined in all source files named SRCFILE. */
6265 collect_file_symbol_completion_matches (completion_tracker
&tracker
,
6266 complete_symbol_mode mode
,
6267 symbol_name_match_type name_match_type
,
6268 const char *text
, const char *word
,
6269 const char *srcfile
)
6271 /* The symbol we are completing on. Points in same buffer as text. */
6272 const char *sym_text
;
6274 /* Now look for the symbol we are supposed to complete on.
6275 FIXME: This should be language-specific. */
6276 if (mode
== complete_symbol_mode::LINESPEC
)
6282 const char *quote_pos
= NULL
;
6284 /* First see if this is a quoted string. */
6286 for (p
= text
; *p
!= '\0'; ++p
)
6288 if (quote_found
!= '\0')
6290 if (*p
== quote_found
)
6291 /* Found close quote. */
6293 else if (*p
== '\\' && p
[1] == quote_found
)
6294 /* A backslash followed by the quote character
6295 doesn't end the string. */
6298 else if (*p
== '\'' || *p
== '"')
6304 if (quote_found
== '\'')
6305 /* A string within single quotes can be a symbol, so complete on it. */
6306 sym_text
= quote_pos
+ 1;
6307 else if (quote_found
== '"')
6308 /* A double-quoted string is never a symbol, nor does it make sense
6309 to complete it any other way. */
6315 /* Not a quoted string. */
6316 sym_text
= language_search_unquoted_string (text
, p
);
6320 lookup_name_info
lookup_name (sym_text
, name_match_type
, true);
6322 /* Go through symtabs for SRCFILE and check the externs and statics
6323 for symbols which match. */
6324 iterate_over_symtabs (current_program_space
, srcfile
, [&] (symtab
*s
)
6326 add_symtab_completions (s
->compunit (),
6327 tracker
, mode
, lookup_name
,
6328 sym_text
, word
, TYPE_CODE_UNDEF
);
6333 /* A helper function for make_source_files_completion_list. It adds
6334 another file name to a list of possible completions, growing the
6335 list as necessary. */
6338 add_filename_to_list (const char *fname
, const char *text
, const char *word
,
6339 completion_list
*list
)
6341 list
->emplace_back (make_completion_match_str (fname
, text
, word
));
6345 not_interesting_fname (const char *fname
)
6347 static const char *illegal_aliens
[] = {
6348 "_globals_", /* inserted by coff_symtab_read */
6353 for (i
= 0; illegal_aliens
[i
]; i
++)
6355 if (filename_cmp (fname
, illegal_aliens
[i
]) == 0)
6361 /* An object of this type is passed as the callback argument to
6362 map_partial_symbol_filenames. */
6363 struct add_partial_filename_data
6365 struct filename_seen_cache
*filename_seen_cache
;
6369 completion_list
*list
;
6371 void operator() (const char *filename
, const char *fullname
);
6374 /* A callback for map_partial_symbol_filenames. */
6377 add_partial_filename_data::operator() (const char *filename
,
6378 const char *fullname
)
6380 if (not_interesting_fname (filename
))
6382 if (!filename_seen_cache
->seen (filename
)
6383 && filename_ncmp (filename
, text
, text_len
) == 0)
6385 /* This file matches for a completion; add it to the
6386 current list of matches. */
6387 add_filename_to_list (filename
, text
, word
, list
);
6391 const char *base_name
= lbasename (filename
);
6393 if (base_name
!= filename
6394 && !filename_seen_cache
->seen (base_name
)
6395 && filename_ncmp (base_name
, text
, text_len
) == 0)
6396 add_filename_to_list (base_name
, text
, word
, list
);
6400 /* Return a list of all source files whose names begin with matching
6401 TEXT. The file names are looked up in the symbol tables of this
6405 make_source_files_completion_list (const char *text
, const char *word
)
6407 size_t text_len
= strlen (text
);
6408 completion_list list
;
6409 const char *base_name
;
6410 struct add_partial_filename_data datum
;
6412 if (!have_full_symbols (current_program_space
)
6413 && !have_partial_symbols (current_program_space
))
6416 filename_seen_cache filenames_seen
;
6418 for (objfile
*objfile
: current_program_space
->objfiles ())
6420 for (compunit_symtab
*cu
: objfile
->compunits ())
6422 for (symtab
*s
: cu
->filetabs ())
6424 if (not_interesting_fname (s
->filename
))
6426 if (!filenames_seen
.seen (s
->filename
)
6427 && filename_ncmp (s
->filename
, text
, text_len
) == 0)
6429 /* This file matches for a completion; add it to the current
6431 add_filename_to_list (s
->filename
, text
, word
, &list
);
6435 /* NOTE: We allow the user to type a base name when the
6436 debug info records leading directories, but not the other
6437 way around. This is what subroutines of breakpoint
6438 command do when they parse file names. */
6439 base_name
= lbasename (s
->filename
);
6440 if (base_name
!= s
->filename
6441 && !filenames_seen
.seen (base_name
)
6442 && filename_ncmp (base_name
, text
, text_len
) == 0)
6443 add_filename_to_list (base_name
, text
, word
, &list
);
6449 datum
.filename_seen_cache
= &filenames_seen
;
6452 datum
.text_len
= text_len
;
6454 map_symbol_filenames (datum
, false /*need_fullname*/);
6461 /* Return the "main_info" object for the current program space. If
6462 the object has not yet been created, create it and fill in some
6466 get_main_info (program_space
*pspace
)
6468 main_info
*info
= main_progspace_key
.get (pspace
);
6472 /* It may seem strange to store the main name in the progspace
6473 and also in whatever objfile happens to see a main name in
6474 its debug info. The reason for this is mainly historical:
6475 gdb returned "main" as the name even if no function named
6476 "main" was defined the program; and this approach lets us
6477 keep compatibility. */
6478 info
= main_progspace_key
.emplace (pspace
);
6485 set_main_name (program_space
*pspace
, const char *name
, enum language lang
)
6487 main_info
*info
= get_main_info (pspace
);
6489 if (!info
->name_of_main
.empty ())
6491 info
->name_of_main
.clear ();
6492 info
->language_of_main
= language_unknown
;
6496 info
->name_of_main
= name
;
6497 info
->language_of_main
= lang
;
6501 /* Deduce the name of the main procedure, and set NAME_OF_MAIN
6505 find_main_name (void)
6507 const char *new_main_name
;
6508 program_space
*pspace
= current_program_space
;
6510 /* First check the objfiles to see whether a debuginfo reader has
6511 picked up the appropriate main name. Historically the main name
6512 was found in a more or less random way; this approach instead
6513 relies on the order of objfile creation -- which still isn't
6514 guaranteed to get the correct answer, but is just probably more
6516 for (objfile
*objfile
: current_program_space
->objfiles ())
6518 objfile
->compute_main_name ();
6520 if (objfile
->per_bfd
->name_of_main
!= NULL
)
6522 set_main_name (pspace
,
6523 objfile
->per_bfd
->name_of_main
,
6524 objfile
->per_bfd
->language_of_main
);
6529 /* Try to see if the main procedure is in Ada. */
6530 /* FIXME: brobecker/2005-03-07: Another way of doing this would
6531 be to add a new method in the language vector, and call this
6532 method for each language until one of them returns a non-empty
6533 name. This would allow us to remove this hard-coded call to
6534 an Ada function. It is not clear that this is a better approach
6535 at this point, because all methods need to be written in a way
6536 such that false positives never be returned. For instance, it is
6537 important that a method does not return a wrong name for the main
6538 procedure if the main procedure is actually written in a different
6539 language. It is easy to guaranty this with Ada, since we use a
6540 special symbol generated only when the main in Ada to find the name
6541 of the main procedure. It is difficult however to see how this can
6542 be guaranteed for languages such as C, for instance. This suggests
6543 that order of call for these methods becomes important, which means
6544 a more complicated approach. */
6545 new_main_name
= ada_main_name ();
6546 if (new_main_name
!= NULL
)
6548 set_main_name (pspace
, new_main_name
, language_ada
);
6552 new_main_name
= d_main_name ();
6553 if (new_main_name
!= NULL
)
6555 set_main_name (pspace
, new_main_name
, language_d
);
6559 new_main_name
= go_main_name ();
6560 if (new_main_name
!= NULL
)
6562 set_main_name (pspace
, new_main_name
, language_go
);
6566 new_main_name
= pascal_main_name ();
6567 if (new_main_name
!= NULL
)
6569 set_main_name (pspace
, new_main_name
, language_pascal
);
6573 /* The languages above didn't identify the name of the main procedure.
6574 Fallback to "main". */
6576 /* Try to find language for main in psymtabs. */
6577 bool symbol_found_p
= false;
6578 gdbarch_iterate_over_objfiles_in_search_order
6579 (current_inferior ()->arch (),
6580 [&symbol_found_p
, pspace
] (objfile
*obj
)
6583 = obj
->lookup_global_symbol_language ("main",
6584 SEARCH_FUNCTION_DOMAIN
,
6588 set_main_name (pspace
, "main", lang
);
6598 set_main_name (pspace
, "main", language_unknown
);
6606 main_info
*info
= get_main_info (current_program_space
);
6608 if (info
->name_of_main
.empty ())
6611 return info
->name_of_main
.c_str ();
6614 /* Return the language of the main function. If it is not known,
6615 return language_unknown. */
6618 main_language (void)
6620 main_info
*info
= get_main_info (current_program_space
);
6622 if (info
->name_of_main
.empty ())
6625 return info
->language_of_main
;
6630 /* The next index to hand out in response to a registration request. */
6632 static int next_aclass_value
= LOC_FINAL_VALUE
;
6634 /* The maximum number of "aclass" registrations we support. This is
6635 constant for convenience. */
6636 #define MAX_SYMBOL_IMPLS (LOC_FINAL_VALUE + 11)
6638 /* The objects representing the various "aclass" values. The elements
6639 from 0 up to LOC_FINAL_VALUE-1 represent themselves, and subsequent
6640 elements are those registered at gdb initialization time. */
6642 static struct symbol_impl symbol_impl
[MAX_SYMBOL_IMPLS
];
6644 /* The globally visible pointer. This is separate from 'symbol_impl'
6645 so that it can be const. */
6647 gdb::array_view
<const struct symbol_impl
> symbol_impls (symbol_impl
);
6649 /* Make sure we saved enough room in struct symbol. */
6651 static_assert (MAX_SYMBOL_IMPLS
<= (1 << SYMBOL_ACLASS_BITS
));
6653 /* Register a computed symbol type. ACLASS must be LOC_COMPUTED. OPS
6654 is the ops vector associated with this index. This returns the new
6655 index, which should be used as the aclass_index field for symbols
6659 register_symbol_computed_impl (enum address_class aclass
,
6660 const struct symbol_computed_ops
*ops
)
6662 int result
= next_aclass_value
++;
6664 gdb_assert (aclass
== LOC_COMPUTED
);
6665 gdb_assert (result
< MAX_SYMBOL_IMPLS
);
6666 symbol_impl
[result
].aclass
= aclass
;
6667 symbol_impl
[result
].ops_computed
= ops
;
6669 /* Sanity check OPS. */
6670 gdb_assert (ops
!= NULL
);
6671 gdb_assert (ops
->tracepoint_var_ref
!= NULL
);
6672 gdb_assert (ops
->describe_location
!= NULL
);
6673 gdb_assert (ops
->get_symbol_read_needs
!= NULL
);
6674 gdb_assert (ops
->read_variable
!= NULL
);
6679 /* Register a function with frame base type. ACLASS must be LOC_BLOCK.
6680 OPS is the ops vector associated with this index. This returns the
6681 new index, which should be used as the aclass_index field for symbols
6685 register_symbol_block_impl (enum address_class aclass
,
6686 const struct symbol_block_ops
*ops
)
6688 int result
= next_aclass_value
++;
6690 gdb_assert (aclass
== LOC_BLOCK
);
6691 gdb_assert (result
< MAX_SYMBOL_IMPLS
);
6692 symbol_impl
[result
].aclass
= aclass
;
6693 symbol_impl
[result
].ops_block
= ops
;
6695 /* Sanity check OPS. */
6696 gdb_assert (ops
!= NULL
);
6697 gdb_assert (ops
->find_frame_base_location
!= nullptr
6698 || ops
->get_block_value
!= nullptr);
6703 /* Register a register symbol type. ACLASS must be LOC_REGISTER or
6704 LOC_REGPARM_ADDR. OPS is the register ops vector associated with
6705 this index. This returns the new index, which should be used as
6706 the aclass_index field for symbols of this type. */
6709 register_symbol_register_impl (enum address_class aclass
,
6710 const struct symbol_register_ops
*ops
)
6712 int result
= next_aclass_value
++;
6714 gdb_assert (aclass
== LOC_REGISTER
|| aclass
== LOC_REGPARM_ADDR
);
6715 gdb_assert (result
< MAX_SYMBOL_IMPLS
);
6716 symbol_impl
[result
].aclass
= aclass
;
6717 symbol_impl
[result
].ops_register
= ops
;
6722 /* Initialize elements of 'symbol_impl' for the constants in enum
6726 initialize_ordinary_address_classes (void)
6730 for (i
= 0; i
< LOC_FINAL_VALUE
; ++i
)
6731 symbol_impl
[i
].aclass
= (enum address_class
) i
;
6739 symbol::objfile () const
6741 gdb_assert (is_objfile_owned ());
6742 return owner
.symtab
->compunit ()->objfile ();
6748 symbol::arch () const
6750 if (!is_objfile_owned ())
6752 return owner
.symtab
->compunit ()->objfile ()->arch ();
6758 symbol::symtab () const
6760 gdb_assert (is_objfile_owned ());
6761 return owner
.symtab
;
6767 symbol::set_symtab (struct symtab
*symtab
)
6769 gdb_assert (is_objfile_owned ());
6770 owner
.symtab
= symtab
;
6776 symbol::get_maybe_copied_address () const
6778 gdb_assert (this->maybe_copied
);
6779 gdb_assert (this->aclass () == LOC_STATIC
);
6781 const char *linkage_name
= this->linkage_name ();
6782 bound_minimal_symbol minsym
6783 = lookup_minimal_symbol_linkage (this->objfile ()->pspace (), linkage_name
,
6785 if (minsym
.minsym
!= nullptr)
6786 return minsym
.value_address ();
6788 return this->m_value
.address
;
6794 minimal_symbol::get_maybe_copied_address (objfile
*objf
) const
6796 gdb_assert (this->maybe_copied (objf
));
6797 gdb_assert ((objf
->flags
& OBJF_MAINLINE
) == 0);
6799 const char *linkage_name
= this->linkage_name ();
6800 bound_minimal_symbol found
6801 = lookup_minimal_symbol_linkage (objf
->pspace (), linkage_name
,
6803 if (found
.minsym
!= nullptr)
6804 return found
.value_address ();
6806 return (this->m_value
.address
6807 + objf
->section_offsets
[this->section_index ()]);
6812 /* Hold the sub-commands of 'info module'. */
6814 static struct cmd_list_element
*info_module_cmdlist
= NULL
;
6818 std::vector
<module_symbol_search
>
6819 search_module_symbols (const char *module_regexp
, const char *regexp
,
6820 const char *type_regexp
, domain_search_flags kind
)
6822 std::vector
<module_symbol_search
> results
;
6824 /* Search for all modules matching MODULE_REGEXP. */
6825 global_symbol_searcher
spec1 (SEARCH_MODULE_DOMAIN
, module_regexp
);
6826 spec1
.set_exclude_minsyms (true);
6827 std::vector
<symbol_search
> modules
= spec1
.search ();
6829 /* Now search for all symbols of the required KIND matching the required
6830 regular expressions. We figure out which ones are in which modules
6832 global_symbol_searcher
spec2 (kind
, regexp
);
6833 spec2
.set_symbol_type_regexp (type_regexp
);
6834 spec2
.set_exclude_minsyms (true);
6835 std::vector
<symbol_search
> symbols
= spec2
.search ();
6837 /* Now iterate over all MODULES, checking to see which items from
6838 SYMBOLS are in each module. */
6839 for (const symbol_search
&p
: modules
)
6843 /* This is a module. */
6844 gdb_assert (p
.symbol
!= nullptr);
6846 std::string prefix
= p
.symbol
->print_name ();
6849 for (const symbol_search
&q
: symbols
)
6851 if (q
.symbol
== nullptr)
6854 if (strncmp (q
.symbol
->print_name (), prefix
.c_str (),
6855 prefix
.size ()) != 0)
6858 results
.push_back ({p
, q
});
6865 /* Implement the core of both 'info module functions' and 'info module
6869 info_module_subcommand (bool quiet
, const char *module_regexp
,
6870 const char *regexp
, const char *type_regexp
,
6871 domain_search_flags kind
)
6873 gdb_assert (kind
== SEARCH_FUNCTION_DOMAIN
|| kind
== SEARCH_VAR_DOMAIN
);
6875 /* Print a header line. Don't build the header line bit by bit as this
6876 prevents internationalisation. */
6879 if (module_regexp
== nullptr)
6881 if (type_regexp
== nullptr)
6883 if (regexp
== nullptr)
6884 gdb_printf ((kind
== SEARCH_VAR_DOMAIN
6885 ? _("All variables in all modules:")
6886 : _("All functions in all modules:")));
6889 ((kind
== SEARCH_VAR_DOMAIN
6890 ? _("All variables matching regular expression"
6891 " \"%s\" in all modules:")
6892 : _("All functions matching regular expression"
6893 " \"%s\" in all modules:")),
6898 if (regexp
== nullptr)
6900 ((kind
== SEARCH_VAR_DOMAIN
6901 ? _("All variables with type matching regular "
6902 "expression \"%s\" in all modules:")
6903 : _("All functions with type matching regular "
6904 "expression \"%s\" in all modules:")),
6908 ((kind
== SEARCH_VAR_DOMAIN
6909 ? _("All variables matching regular expression "
6910 "\"%s\",\n\twith type matching regular "
6911 "expression \"%s\" in all modules:")
6912 : _("All functions matching regular expression "
6913 "\"%s\",\n\twith type matching regular "
6914 "expression \"%s\" in all modules:")),
6915 regexp
, type_regexp
);
6920 if (type_regexp
== nullptr)
6922 if (regexp
== nullptr)
6924 ((kind
== SEARCH_VAR_DOMAIN
6925 ? _("All variables in all modules matching regular "
6926 "expression \"%s\":")
6927 : _("All functions in all modules matching regular "
6928 "expression \"%s\":")),
6932 ((kind
== SEARCH_VAR_DOMAIN
6933 ? _("All variables matching regular expression "
6934 "\"%s\",\n\tin all modules matching regular "
6935 "expression \"%s\":")
6936 : _("All functions matching regular expression "
6937 "\"%s\",\n\tin all modules matching regular "
6938 "expression \"%s\":")),
6939 regexp
, module_regexp
);
6943 if (regexp
== nullptr)
6945 ((kind
== SEARCH_VAR_DOMAIN
6946 ? _("All variables with type matching regular "
6947 "expression \"%s\"\n\tin all modules matching "
6948 "regular expression \"%s\":")
6949 : _("All functions with type matching regular "
6950 "expression \"%s\"\n\tin all modules matching "
6951 "regular expression \"%s\":")),
6952 type_regexp
, module_regexp
);
6955 ((kind
== SEARCH_VAR_DOMAIN
6956 ? _("All variables matching regular expression "
6957 "\"%s\",\n\twith type matching regular expression "
6958 "\"%s\",\n\tin all modules matching regular "
6959 "expression \"%s\":")
6960 : _("All functions matching regular expression "
6961 "\"%s\",\n\twith type matching regular expression "
6962 "\"%s\",\n\tin all modules matching regular "
6963 "expression \"%s\":")),
6964 regexp
, type_regexp
, module_regexp
);
6970 /* Find all symbols of type KIND matching the given regular expressions
6971 along with the symbols for the modules in which those symbols
6973 std::vector
<module_symbol_search
> module_symbols
6974 = search_module_symbols (module_regexp
, regexp
, type_regexp
, kind
);
6976 std::sort (module_symbols
.begin (), module_symbols
.end (),
6977 [] (const module_symbol_search
&a
, const module_symbol_search
&b
)
6979 if (a
.first
< b
.first
)
6981 else if (a
.first
== b
.first
)
6982 return a
.second
< b
.second
;
6987 const char *last_filename
= "";
6988 const symbol
*last_module_symbol
= nullptr;
6989 for (const module_symbol_search
&ms
: module_symbols
)
6991 const symbol_search
&p
= ms
.first
;
6992 const symbol_search
&q
= ms
.second
;
6994 gdb_assert (q
.symbol
!= nullptr);
6996 if (last_module_symbol
!= p
.symbol
)
6999 gdb_printf (_("Module \"%s\":\n"), p
.symbol
->print_name ());
7000 last_module_symbol
= p
.symbol
;
7004 print_symbol_info (q
.symbol
, q
.block
, last_filename
);
7006 = symtab_to_filename_for_display (q
.symbol
->symtab ());
7010 /* Hold the option values for the 'info module .....' sub-commands. */
7012 struct info_modules_var_func_options
7015 std::string type_regexp
;
7016 std::string module_regexp
;
7019 /* The options used by 'info module variables' and 'info module functions'
7022 static const gdb::option::option_def info_modules_var_func_options_defs
[] = {
7023 gdb::option::boolean_option_def
<info_modules_var_func_options
> {
7025 [] (info_modules_var_func_options
*opt
) { return &opt
->quiet
; },
7026 nullptr, /* show_cmd_cb */
7027 nullptr /* set_doc */
7030 gdb::option::string_option_def
<info_modules_var_func_options
> {
7032 [] (info_modules_var_func_options
*opt
) { return &opt
->type_regexp
; },
7033 nullptr, /* show_cmd_cb */
7034 nullptr /* set_doc */
7037 gdb::option::string_option_def
<info_modules_var_func_options
> {
7039 [] (info_modules_var_func_options
*opt
) { return &opt
->module_regexp
; },
7040 nullptr, /* show_cmd_cb */
7041 nullptr /* set_doc */
7045 /* Return the option group used by the 'info module ...' sub-commands. */
7047 static inline gdb::option::option_def_group
7048 make_info_modules_var_func_options_def_group
7049 (info_modules_var_func_options
*opts
)
7051 return {{info_modules_var_func_options_defs
}, opts
};
7054 /* Implements the 'info module functions' command. */
7057 info_module_functions_command (const char *args
, int from_tty
)
7059 info_modules_var_func_options opts
;
7060 auto grp
= make_info_modules_var_func_options_def_group (&opts
);
7061 gdb::option::process_options
7062 (&args
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, grp
);
7063 if (args
!= nullptr && *args
== '\0')
7066 info_module_subcommand
7068 opts
.module_regexp
.empty () ? nullptr : opts
.module_regexp
.c_str (), args
,
7069 opts
.type_regexp
.empty () ? nullptr : opts
.type_regexp
.c_str (),
7070 SEARCH_FUNCTION_DOMAIN
);
7073 /* Implements the 'info module variables' command. */
7076 info_module_variables_command (const char *args
, int from_tty
)
7078 info_modules_var_func_options opts
;
7079 auto grp
= make_info_modules_var_func_options_def_group (&opts
);
7080 gdb::option::process_options
7081 (&args
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, grp
);
7082 if (args
!= nullptr && *args
== '\0')
7085 info_module_subcommand
7087 opts
.module_regexp
.empty () ? nullptr : opts
.module_regexp
.c_str (), args
,
7088 opts
.type_regexp
.empty () ? nullptr : opts
.type_regexp
.c_str (),
7092 /* Command completer for 'info module ...' sub-commands. */
7095 info_module_var_func_command_completer (struct cmd_list_element
*ignore
,
7096 completion_tracker
&tracker
,
7098 const char * /* word */)
7101 const auto group
= make_info_modules_var_func_options_def_group (nullptr);
7102 if (gdb::option::complete_options
7103 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
7106 const char *word
= advance_to_expression_complete_word_point (tracker
, text
);
7107 symbol_completer (ignore
, tracker
, text
, word
);
7112 INIT_GDB_FILE (symtab
)
7114 cmd_list_element
*c
;
7116 initialize_ordinary_address_classes ();
7118 c
= add_info ("variables", info_variables_command
,
7119 info_print_args_help (_("\
7120 All global and static variable names or those matching REGEXPs.\n\
7121 Usage: info variables [-q] [-n] [-t TYPEREGEXP] [NAMEREGEXP]\n\
7122 Prints the global and static variables.\n"),
7123 _("global and static variables"),
7125 set_cmd_completer_handle_brkchars (c
, info_vars_funcs_command_completer
);
7127 c
= add_info ("functions", info_functions_command
,
7128 info_print_args_help (_("\
7129 All function names or those matching REGEXPs.\n\
7130 Usage: info functions [-q] [-n] [-t TYPEREGEXP] [NAMEREGEXP]\n\
7131 Prints the functions.\n"),
7134 set_cmd_completer_handle_brkchars (c
, info_vars_funcs_command_completer
);
7136 c
= add_info ("types", info_types_command
, _("\
7137 All type names, or those matching REGEXP.\n\
7138 Usage: info types [-q] [REGEXP]\n\
7139 Print information about all types matching REGEXP, or all types if no\n\
7140 REGEXP is given. The optional flag -q disables printing of headers."));
7141 set_cmd_completer_handle_brkchars (c
, info_types_command_completer
);
7143 const auto info_sources_opts
7144 = make_info_sources_options_def_group (nullptr);
7146 static std::string info_sources_help
7147 = gdb::option::build_help (_("\
7148 All source files in the program or those matching REGEXP.\n\
7149 Usage: info sources [OPTION]... [REGEXP]\n\
7150 By default, REGEXP is used to match anywhere in the filename.\n\
7156 c
= add_info ("sources", info_sources_command
, info_sources_help
.c_str ());
7157 set_cmd_completer_handle_brkchars (c
, info_sources_command_completer
);
7159 c
= add_info ("modules", info_modules_command
,
7160 _("All module names, or those matching REGEXP."));
7161 set_cmd_completer_handle_brkchars (c
, info_types_command_completer
);
7163 add_info ("main", info_main_command
,
7164 _("Get main symbol to identify entry point into program."));
7166 add_basic_prefix_cmd ("module", class_info
, _("\
7167 Print information about modules."),
7168 &info_module_cmdlist
, 0, &infolist
);
7170 c
= add_cmd ("functions", class_info
, info_module_functions_command
, _("\
7171 Display functions arranged by modules.\n\
7172 Usage: info module functions [-q] [-m MODREGEXP] [-t TYPEREGEXP] [REGEXP]\n\
7173 Print a summary of all functions within each Fortran module, grouped by\n\
7174 module and file. For each function the line on which the function is\n\
7175 defined is given along with the type signature and name of the function.\n\
7177 If REGEXP is provided then only functions whose name matches REGEXP are\n\
7178 listed. If MODREGEXP is provided then only functions in modules matching\n\
7179 MODREGEXP are listed. If TYPEREGEXP is given then only functions whose\n\
7180 type signature matches TYPEREGEXP are listed.\n\
7182 The -q flag suppresses printing some header information."),
7183 &info_module_cmdlist
);
7184 set_cmd_completer_handle_brkchars
7185 (c
, info_module_var_func_command_completer
);
7187 c
= add_cmd ("variables", class_info
, info_module_variables_command
, _("\
7188 Display variables arranged by modules.\n\
7189 Usage: info module variables [-q] [-m MODREGEXP] [-t TYPEREGEXP] [REGEXP]\n\
7190 Print a summary of all variables within each Fortran module, grouped by\n\
7191 module and file. For each variable the line on which the variable is\n\
7192 defined is given along with the type and name of the variable.\n\
7194 If REGEXP is provided then only variables whose name matches REGEXP are\n\
7195 listed. If MODREGEXP is provided then only variables in modules matching\n\
7196 MODREGEXP are listed. If TYPEREGEXP is given then only variables whose\n\
7197 type matches TYPEREGEXP are listed.\n\
7199 The -q flag suppresses printing some header information."),
7200 &info_module_cmdlist
);
7201 set_cmd_completer_handle_brkchars
7202 (c
, info_module_var_func_command_completer
);
7204 add_com ("rbreak", class_breakpoint
, rbreak_command
,
7205 _("Set a breakpoint for all functions matching REGEXP."));
7207 add_setshow_enum_cmd ("multiple-symbols", no_class
,
7208 multiple_symbols_modes
, &multiple_symbols_mode
,
7210 Set how the debugger handles ambiguities in expressions."), _("\
7211 Show how the debugger handles ambiguities in expressions."), _("\
7212 Valid values are \"ask\", \"all\", \"cancel\", and the default is \"all\"."),
7213 NULL
, NULL
, &setlist
, &showlist
);
7215 add_setshow_boolean_cmd ("basenames-may-differ", class_obscure
,
7216 &basenames_may_differ
, _("\
7217 Set whether a source file may have multiple base names."), _("\
7218 Show whether a source file may have multiple base names."), _("\
7219 (A \"base name\" is the name of a file with the directory part removed.\n\
7220 Example: The base name of \"/home/user/hello.c\" is \"hello.c\".)\n\
7221 If set, GDB will canonicalize file names (e.g., expand symlinks)\n\
7222 before comparing them. Canonicalization is an expensive operation,\n\
7223 but it allows the same file be known by more than one base name.\n\
7224 If not set (the default), all source files are assumed to have just\n\
7225 one base name, and gdb will do file name comparisons more efficiently."),
7227 &setlist
, &showlist
);
7229 add_setshow_zuinteger_cmd ("symtab-create", no_class
, &symtab_create_debug
,
7230 _("Set debugging of symbol table creation."),
7231 _("Show debugging of symbol table creation."), _("\
7232 When enabled (non-zero), debugging messages are printed when building\n\
7233 symbol tables. A value of 1 (one) normally provides enough information.\n\
7234 A value greater than 1 provides more verbose information."),
7237 &setdebuglist
, &showdebuglist
);
7239 add_setshow_zuinteger_cmd ("symbol-lookup", no_class
, &symbol_lookup_debug
,
7241 Set debugging of symbol lookup."), _("\
7242 Show debugging of symbol lookup."), _("\
7243 When enabled (non-zero), symbol lookups are logged."),
7245 &setdebuglist
, &showdebuglist
);
7247 add_setshow_zuinteger_cmd ("symbol-cache-size", no_class
,
7248 &new_symbol_cache_size
,
7249 _("Set the size of the symbol cache."),
7250 _("Show the size of the symbol cache."), _("\
7251 The size of the symbol cache.\n\
7252 If zero then the symbol cache is disabled."),
7253 set_symbol_cache_size_handler
, NULL
,
7254 &maintenance_set_cmdlist
,
7255 &maintenance_show_cmdlist
);
7257 add_setshow_boolean_cmd ("ignore-prologue-end-flag", no_class
,
7258 &ignore_prologue_end_flag
,
7259 _("Set if the PROLOGUE-END flag is ignored."),
7260 _("Show if the PROLOGUE-END flag is ignored."),
7262 The PROLOGUE-END flag from the line-table entries is used to place\n\
7263 breakpoints past the prologue of functions. Disabling its use forces\n\
7264 the use of prologue scanners."),
7266 &maintenance_set_cmdlist
,
7267 &maintenance_show_cmdlist
);
7270 add_cmd ("symbol-cache", class_maintenance
, maintenance_print_symbol_cache
,
7271 _("Dump the symbol cache for each program space."),
7272 &maintenanceprintlist
);
7274 add_cmd ("symbol-cache-statistics", class_maintenance
,
7275 maintenance_print_symbol_cache_statistics
,
7276 _("Print symbol cache statistics for each program space."),
7277 &maintenanceprintlist
);
7279 cmd_list_element
*maintenance_flush_symbol_cache_cmd
7280 = add_cmd ("symbol-cache", class_maintenance
,
7281 maintenance_flush_symbol_cache
,
7282 _("Flush the symbol cache for each program space."),
7283 &maintenanceflushlist
);
7284 c
= add_alias_cmd ("flush-symbol-cache", maintenance_flush_symbol_cache_cmd
,
7285 class_maintenance
, 0, &maintenancelist
);
7286 deprecate_cmd (c
, "maintenance flush symbol-cache");
7288 gdb::observers::new_objfile
.attach (symtab_new_objfile_observer
, "symtab");
7289 gdb::observers::all_objfiles_removed
.attach (symtab_all_objfiles_removed
,
7291 gdb::observers::free_objfile
.attach (symtab_free_objfile_observer
, "symtab");