Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@efficios.com>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@polymtl.ca>
Subject: [PATCH 07/11] gdb: split iterate_over_minimal_symbols in for_each_minimal_symbol and find_minimal_symbol
Date: Thu, 16 Apr 2026 16:16:17 -0400	[thread overview]
Message-ID: <20260416202408.422441-8-simon.marchi@efficios.com> (raw)
In-Reply-To: <20260416202408.422441-1-simon.marchi@efficios.com>

From: Simon Marchi <simon.marchi@polymtl.ca>

Based on the same rationale as the previous patches, split
iterate_over_minimal_symbols in two.

Implement for_each_minimal_symbol using find_minimal_symbol, since that
one is really not trivial.

Change-Id: Ie02e67278359454f7aa583200ec68d2f429f7ebe
---
 gdb/linespec.c | 35 ++++++++++++++++-------------------
 gdb/minsyms.c  | 25 ++++++++++++++++++++-----
 gdb/minsyms.h  | 37 +++++++++++++++++++++++++++++--------
 gdb/symtab.c   | 18 +++++++++---------
 4 files changed, 74 insertions(+), 41 deletions(-)

diff --git a/gdb/linespec.c b/gdb/linespec.c
index 4ea6d597a093..4170d4ecf2c4 100644
--- a/gdb/linespec.c
+++ b/gdb/linespec.c
@@ -4150,33 +4150,30 @@ search_minsyms_for_name (struct collect_info *info,
 	  set_current_program_space (pspace);
 
 	  for (objfile &objfile : pspace->objfiles ())
-	    {
-	      iterate_over_minimal_symbols (&objfile, name,
-					    [&] (struct minimal_symbol *msym)
-					    {
-					      add_minsym (msym, &objfile, nullptr,
-							  info->state->list_mode,
-							  &minsyms);
-					      return false;
-					    });
-	    }
+	    for_each_minimal_symbol (&objfile, name,
+				     [&] (minimal_symbol *msym)
+				       {
+					 add_minsym (msym, &objfile, nullptr,
+						     info->state->list_mode,
+						     &minsyms);
+				       });
 	}
     }
   else
     {
-      program_space *pspace = symtab->compunit ()->objfile ()->pspace ();
+      objfile &objfile = *symtab->compunit ()->objfile ();
+      program_space *pspace = objfile.pspace ();
 
       if (search_pspace == NULL || pspace == search_pspace)
 	{
 	  set_current_program_space (pspace);
-	  iterate_over_minimal_symbols
-	    (symtab->compunit ()->objfile (), name,
-	     [&] (struct minimal_symbol *msym)
-	       {
-		 add_minsym (msym, symtab->compunit ()->objfile (), symtab,
-			     info->state->list_mode, &minsyms);
-		 return false;
-	       });
+	  for_each_minimal_symbol (&objfile, name,
+				   [&] (minimal_symbol *msym)
+				     {
+				       add_minsym (msym, &objfile, symtab,
+						   info->state->list_mode,
+						   &minsyms);
+				     });
 	}
     }
 
diff --git a/gdb/minsyms.c b/gdb/minsyms.c
index 0d6ea53aecf6..4eafc7890478 100644
--- a/gdb/minsyms.c
+++ b/gdb/minsyms.c
@@ -496,9 +496,22 @@ linkage_name_str (const lookup_name_info &lookup_name)
 /* See minsyms.h.  */
 
 void
-iterate_over_minimal_symbols
-    (struct objfile *objf, const lookup_name_info &lookup_name,
-     gdb::function_view<bool (struct minimal_symbol *)> callback)
+for_each_minimal_symbol (struct objfile *objf, const lookup_name_info &name,
+			 for_each_minimal_symbol_callback_ftype callback)
+{
+  find_minimal_symbol (objf, name,
+		       [&] (struct minimal_symbol *msym)
+			 {
+			   callback (msym);
+			   return false;
+			 });
+}
+
+/* See minsyms.h.  */
+
+minimal_symbol *
+find_minimal_symbol (struct objfile *objf, const lookup_name_info &lookup_name,
+		     find_minimal_symbol_callback_ftype callback)
 {
   /* The first pass is over the ordinary hash table.  */
     {
@@ -515,7 +528,7 @@ iterate_over_minimal_symbols
 	{
 	  if (mangled_cmp (iter->linkage_name (), name) == 0)
 	    if (callback (iter))
-	      return;
+	      return iter;
 	}
     }
 
@@ -539,8 +552,10 @@ iterate_over_minimal_symbols
 	   iter = iter->demangled_hash_next)
 	if (name_match (iter->search_name (), lookup_name, NULL))
 	  if (callback (iter))
-	    return;
+	    return iter;
     }
+
+  return nullptr;
 }
 
 /* See minsyms.h.  */
diff --git a/gdb/minsyms.h b/gdb/minsyms.h
index 8f38cc7137fc..c44d5b20aec8 100644
--- a/gdb/minsyms.h
+++ b/gdb/minsyms.h
@@ -20,6 +20,7 @@
 #ifndef GDB_MINSYMS_H
 #define GDB_MINSYMS_H
 
+#include "gdbsupport/function-view.h"
 #include <deque>
 
 struct program_space;
@@ -279,16 +280,36 @@ bound_minimal_symbol lookup_minimal_symbol_by_pc_section
 
 bound_minimal_symbol lookup_minimal_symbol_by_pc (CORE_ADDR);
 
-/* Iterate over all the minimal symbols in the objfile OBJF which
-   match NAME.  Both the ordinary and demangled names of each symbol
-   are considered.  The caller is responsible for canonicalizing NAME,
-   should that need to be done.
+/* Callback type for function for_each_minimal_symbol.  */
 
-   For each matching symbol, CALLBACK is called with the symbol.  */
+using for_each_minimal_symbol_callback_ftype
+  = gdb::function_view<void (struct minimal_symbol *)>;
 
-void iterate_over_minimal_symbols
-    (struct objfile *objf, const lookup_name_info &name,
-     gdb::function_view<bool (struct minimal_symbol *)> callback);
+/* Call CALLBACK for all minimal symbols in objfile OBJF which match NAME.
+
+   Both the ordinary and demangled names of each symbol are considered.  The
+   caller is responsible for canonicalizing NAME, should that need to be
+   done.  */
+
+void for_each_minimal_symbol (struct objfile *objf,
+			      const lookup_name_info &name,
+			      for_each_minimal_symbol_callback_ftype callback);
+
+/* Callback type for function find_minimal_symbol.  */
+
+using find_minimal_symbol_callback_ftype
+  = gdb::function_view<bool (struct minimal_symbol *)>;
+
+/* Find the first minimal symbol for objfile OBJF which matches NAME and for
+   which CALLBACK returns true.
+
+   Both the ordinary and demangled names of each symbol are considered.  The
+   caller is responsible for canonicalizing NAME, should that need to be
+   done.  */
+
+minimal_symbol *find_minimal_symbol
+  (struct objfile *objf, const lookup_name_info &name,
+   find_minimal_symbol_callback_ftype callback);
 
 /* Compute the upper bound of MINSYM.  The upper bound is the last
    address thought to be part of the symbol.  If the symbol has a
diff --git a/gdb/symtab.c b/gdb/symtab.c
index d22dd81a246a..5a39081dfc54 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -5782,32 +5782,32 @@ find_gnu_ifunc (const symbol *sym)
   struct objfile *objfile = sym->objfile ();
 
   CORE_ADDR address = sym->value_block ()->entry_pc ();
-  minimal_symbol *ifunc = NULL;
-
-  iterate_over_minimal_symbols (objfile, lookup_name,
-				[&] (minimal_symbol *minsym)
+  minimal_symbol *ifunc
+    = find_minimal_symbol (objfile, lookup_name,
+			   [&] (minimal_symbol *minsym)
     {
       if (minsym->type () == mst_text_gnu_ifunc
 	  || minsym->type () == mst_data_gnu_ifunc)
 	{
 	  CORE_ADDR msym_addr = minsym->value_address (objfile);
+
 	  if (minsym->type () == mst_data_gnu_ifunc)
 	    {
 	      struct gdbarch *gdbarch = objfile->arch ();
 	      msym_addr = gdbarch_convert_from_func_ptr_addr
 		(gdbarch, msym_addr, current_inferior ()->top_target ());
 	    }
+
 	  if (msym_addr == address)
-	    {
-	      ifunc = minsym;
-	      return true;
-	    }
+	    return true;
 	}
+
       return false;
     });
 
-  if (ifunc != NULL)
+  if (ifunc != nullptr)
     return {ifunc, objfile};
+
   return {};
 }
 
-- 
2.53.0


  parent reply	other threads:[~2026-04-16 20:25 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-16 20:16 [PATCH 00/11] Readability improvements of some iteration functions Simon Marchi
2026-04-16 20:16 ` [PATCH 01/11] gdb/dwarf: remove unused file_match parameter from dwarf2_base_index_functions::search_one Simon Marchi
2026-04-17 11:11   ` Andrew Burgess
2026-04-16 20:16 ` [PATCH 02/11] gdb: rename search_symtabs_expansion_listener -> compunit_symtab_iteration_callback Simon Marchi
2026-04-17 15:08   ` Simon Marchi
2026-04-17 17:17   ` Tom Tromey
2026-04-17 19:34     ` Simon Marchi
2026-04-16 20:16 ` [PATCH 03/11] gdb: introduce iteration_status enum, use it for search callbacks Simon Marchi
2026-04-17 11:01   ` Andrew Burgess
2026-04-17 14:33     ` Simon Marchi
2026-04-17 14:34   ` Tom Tromey
2026-04-16 20:16 ` [PATCH 04/11] gdb, gdbserver: make iterate_over_lwps_ftype a function_view Simon Marchi
2026-04-17 11:09   ` Andrew Burgess
2026-04-17 14:37     ` Simon Marchi
2026-04-16 20:16 ` [PATCH 05/11] gdb, gdbserver: split iterate_over_lwps in for_each_lwp and find_lwp Simon Marchi
2026-04-17 11:25   ` Andrew Burgess
2026-04-17 14:53     ` Simon Marchi
2026-04-16 20:16 ` [PATCH 06/11] gdb: split iterate_over_threads in for_each_thread and find_thread Simon Marchi
2026-04-17 11:33   ` Andrew Burgess
2026-04-16 20:16 ` Simon Marchi [this message]
2026-04-17 12:13   ` [PATCH 07/11] gdb: split iterate_over_minimal_symbols in for_each_minimal_symbol and find_minimal_symbol Andrew Burgess
2026-04-16 20:16 ` [PATCH 08/11] gdb: split iterate_over_symtabs in for_each_symtab and find_symtab Simon Marchi
2026-04-17 13:31   ` Andrew Burgess
2026-04-17 14:54     ` Simon Marchi
2026-04-16 20:16 ` [PATCH 09/11] gdb: change objfile::map_symtabs_matching_filename to find_symtab_matching_filename Simon Marchi
2026-04-17 13:50   ` Andrew Burgess
2026-04-17 15:03     ` Simon Marchi
2026-04-16 20:16 ` [PATCH 10/11] gdb: make symbol_found_callback_ftype a function_view Simon Marchi
2026-04-17 13:55   ` Andrew Burgess
2026-04-17 15:04     ` Simon Marchi
2026-04-16 20:16 ` [PATCH 11/11] gdb: make iterate_over_symbols return void, rename to for_each_symbol Simon Marchi
2026-04-17 14:05   ` Andrew Burgess
2026-04-17 15:06     ` Simon Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260416202408.422441-8-simon.marchi@efficios.com \
    --to=simon.marchi@efficios.com \
    --cc=gdb-patches@sourceware.org \
    --cc=simon.marchi@polymtl.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox