Make global_symbol_searcher::filenames private

This patch renames global_symbol_searcher::filenames and makes it
private, adding a new method to append a filename to the vector.  This
also cleans up memory management here, removing an alloca from rbreak,
and removing a somewhat ugly SCOPE_EXIT from the Python code, in favor
of having global_symbol_searcher manage the memory itself.

Regression tested on x86-64 Fedora 38.
This commit is contained in:
Tom Tromey
2024-05-24 12:31:45 -06:00
parent 58a628530e
commit c4c093a31f
3 changed files with 25 additions and 34 deletions

View File

@@ -804,10 +804,6 @@ gdbpy_rbreak (PyObject *self, PyObject *args, PyObject *kw)
} }
global_symbol_searcher spec (SEARCH_FUNCTION_DOMAIN, regex); global_symbol_searcher spec (SEARCH_FUNCTION_DOMAIN, regex);
SCOPE_EXIT {
for (const char *elem : spec.filenames)
xfree ((void *) elem);
};
/* The "symtabs" keyword is any Python iterable object that returns /* The "symtabs" keyword is any Python iterable object that returns
a gdb.Symtab on each iteration. If specified, iterate through a gdb.Symtab on each iteration. If specified, iterate through
@@ -852,10 +848,7 @@ gdbpy_rbreak (PyObject *self, PyObject *args, PyObject *kw)
if (filename == NULL) if (filename == NULL)
return NULL; return NULL;
/* Make sure there is a definite place to store the value of spec.add_filename (std::move (filename));
filename before it is released. */
spec.filenames.push_back (nullptr);
spec.filenames.back () = filename.release ();
} }
} }

View File

@@ -4765,16 +4765,17 @@ info_sources_command (const char *args, int from_tty)
true compare only lbasename of FILENAMES. */ true compare only lbasename of FILENAMES. */
static bool static bool
file_matches (const char *file, const std::vector<const char *> &filenames, file_matches (const char *file,
const std::vector<gdb::unique_xmalloc_ptr<char>> &filenames,
bool basenames) bool basenames)
{ {
if (filenames.empty ()) if (filenames.empty ())
return true; return true;
for (const char *name : filenames) for (const auto &name : filenames)
{ {
name = (basenames ? lbasename (name) : name); const char *lname = (basenames ? lbasename (name.get ()) : name.get ());
if (compare_filenames_for_search (file, name)) if (compare_filenames_for_search (file, lname))
return true; return true;
} }
@@ -4867,10 +4868,10 @@ global_symbol_searcher::expand_symtabs
auto do_file_match = [&] (const char *filename, bool basenames) auto do_file_match = [&] (const char *filename, bool basenames)
{ {
return file_matches (filename, filenames, basenames); return file_matches (filename, m_filenames, basenames);
}; };
gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher = nullptr; gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher = nullptr;
if (!filenames.empty ()) if (!m_filenames.empty ())
file_matcher = do_file_match; file_matcher = do_file_match;
objfile->expand_symtabs_matching objfile->expand_symtabs_matching
@@ -4899,7 +4900,7 @@ global_symbol_searcher::expand_symtabs
We only search the objfile the msymbol came from, we no longer search We only search the objfile the msymbol came from, we no longer search
all objfiles. In large programs (1000s of shared libs) searching all all objfiles. In large programs (1000s of shared libs) searching all
objfiles is not worth the pain. */ objfiles is not worth the pain. */
if (filenames.empty () if (m_filenames.empty ()
&& (kind & (SEARCH_VAR_DOMAIN | SEARCH_FUNCTION_DOMAIN)) != 0) && (kind & (SEARCH_VAR_DOMAIN | SEARCH_FUNCTION_DOMAIN)) != 0)
{ {
for (minimal_symbol *msymbol : objfile->msymbols ()) for (minimal_symbol *msymbol : objfile->msymbols ())
@@ -4965,12 +4966,12 @@ global_symbol_searcher::add_matching_symbols
/* Check first sole REAL_SYMTAB->FILENAME. It does /* Check first sole REAL_SYMTAB->FILENAME. It does
not need to be a substring of symtab_to_fullname as not need to be a substring of symtab_to_fullname as
it may contain "./" etc. */ it may contain "./" etc. */
if (!(file_matches (real_symtab->filename, filenames, false) if (!(file_matches (real_symtab->filename, m_filenames, false)
|| ((basenames_may_differ || ((basenames_may_differ
|| file_matches (lbasename (real_symtab->filename), || file_matches (lbasename (real_symtab->filename),
filenames, true)) m_filenames, true))
&& file_matches (symtab_to_fullname (real_symtab), && file_matches (symtab_to_fullname (real_symtab),
filenames, false)))) m_filenames, false))))
continue; continue;
if (!sym->matches (kind)) if (!sym->matches (kind))
@@ -5147,7 +5148,7 @@ global_symbol_searcher::search () const
minimal symbol, as we assume that a minimal symbol does not have a minimal symbol, as we assume that a minimal symbol does not have a
type. */ type. */
if ((found_msymbol if ((found_msymbol
|| (filenames.empty () && (m_kind & SEARCH_VAR_DOMAIN) != 0)) || (m_filenames.empty () && (m_kind & SEARCH_VAR_DOMAIN) != 0))
&& !m_exclude_minsyms && !m_exclude_minsyms
&& !treg.has_value ()) && !treg.has_value ())
{ {
@@ -5556,7 +5557,7 @@ static void
rbreak_command (const char *regexp, int from_tty) rbreak_command (const char *regexp, int from_tty)
{ {
std::string string; std::string string;
const char *file_name = nullptr; gdb::unique_xmalloc_ptr<char> file_name;
if (regexp != nullptr) if (regexp != nullptr)
{ {
@@ -5569,23 +5570,18 @@ rbreak_command (const char *regexp, int from_tty)
if (colon && *(colon + 1) != ':') if (colon && *(colon + 1) != ':')
{ {
int colon_index; int colon_index = colon - regexp;
char *local_name; while (colon_index > 0 && isspace (regexp[colon_index - 1]))
--colon_index;
colon_index = colon - regexp; file_name = make_unique_xstrndup (regexp, colon_index);
local_name = (char *) alloca (colon_index + 1);
memcpy (local_name, regexp, colon_index);
local_name[colon_index--] = 0;
while (isspace (local_name[colon_index]))
local_name[colon_index--] = 0;
file_name = local_name;
regexp = skip_spaces (colon + 1); regexp = skip_spaces (colon + 1);
} }
} }
global_symbol_searcher spec (SEARCH_FUNCTION_DOMAIN, regexp); global_symbol_searcher spec (SEARCH_FUNCTION_DOMAIN, regexp);
if (file_name != nullptr) if (file_name != nullptr)
spec.filenames.push_back (file_name); spec.add_filename (std::move (file_name));
std::vector<symbol_search> symbols = spec.search (); std::vector<symbol_search> symbols = spec.search ();
scoped_rbreak_breakpoints finalize; scoped_rbreak_breakpoints finalize;

View File

@@ -2612,12 +2612,14 @@ public:
removed. */ removed. */
std::vector<symbol_search> search () const; std::vector<symbol_search> search () const;
/* The set of source files to search in for matching symbols. This is /* Add a filename to the list of file names to search. */
currently public so that it can be populated after this object has void add_filename (gdb::unique_xmalloc_ptr<char> filename)
been constructed. */ { m_filenames.push_back (std::move (filename)); }
std::vector<const char *> filenames;
private: private:
/* The set of source files to search in for matching symbols. */
std::vector<gdb::unique_xmalloc_ptr<char>> m_filenames;
/* The kind of symbols are we searching for. /* The kind of symbols are we searching for.
VARIABLES_DOMAIN - Search all symbols, excluding functions, type VARIABLES_DOMAIN - Search all symbols, excluding functions, type
names, and constants (enums). names, and constants (enums).