[binutils-gdb] gdb: parameterize all_threads_safe for the target and ptid

Tankut Baris Aktemur via Gdb-cvs <[email protected]>
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=2f74486ac0325bbee80f5aaf0a4fd5e1ff18274a

commit 2f74486ac0325bbee80f5aaf0a4fd5e1ff18274a
Author: Tankut Baris Aktemur <[email protected]>
Date:   Tue Apr 21 09:11:01 2026 +0200

    gdb: parameterize all_threads_safe for the target and ptid
    
    'all_threads' and 'all_threads_safe' are two range providers to
    iterate over threads.  The former takes optional process target and
    ptid arguments to filter the threads that are being iterated.  The
    latter does not.  Enhance 'all_threads_safe' to also take the optional
    arguments.  Use the new version of the function in two places.
    
    Approved-By: Simon Marchi <[email protected]>

Diff:
---
 gdb/gdbthread.h   | 11 ++++++-----
 gdb/infrun.c      |  8 ++++----
 gdb/remote.c      | 47 +++++++++++++++++++++--------------------------
 gdb/thread-iter.h | 14 ++++++++------
 4 files changed, 39 insertions(+), 41 deletions(-)

diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index 729dcf5ab06..bfafcd61e80 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -863,13 +863,14 @@ all_non_exited_threads (process_stratum_target *proc_target = nullptr,
 	 delete &f;
 */
 
-inline all_threads_safe_range
-all_threads_safe ()
+inline all_matching_threads_safe_range
+all_threads_safe (process_stratum_target *proc_target = nullptr,
+		  ptid_t filter_ptid = minus_one_ptid)
 {
-  all_threads_iterator begin (all_threads_iterator::begin_t {});
-  all_threads_safe_iterator safe_begin (std::move (begin));
+  all_matching_threads_iterator begin (proc_target, filter_ptid);
+  all_matching_threads_safe_iterator safe_begin (std::move (begin));
 
-  return all_threads_safe_range (std::move (safe_begin));
+  return all_matching_threads_safe_range (std::move (safe_begin));
 }
 
 extern int thread_count (process_stratum_target *proc_target);
diff --git a/gdb/infrun.c b/gdb/infrun.c
index b295956f8ea..6401df78e0a 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1239,7 +1239,7 @@ static void
 follow_exec (ptid_t ptid, const char *exec_file_target)
 {
   int pid = ptid.pid ();
-  ptid_t process_ptid;
+  ptid_t process_ptid (pid);
 
   /* Switch terminal for any messages produced e.g. by
      breakpoint_re_set.  */
@@ -1285,8 +1285,9 @@ follow_exec (ptid_t ptid, const char *exec_file_target)
      them.  Deleting them now rather than at the next user-visible
      stop provides a nicer sequence of events for user and MI
      notifications.  */
-  for (thread_info &th : all_threads_safe ())
-    if (th.ptid.pid () == pid && th.ptid != ptid)
+  process_stratum_target *target = current_inferior ()->process_target ();
+  for (thread_info &th : all_threads_safe (target, process_ptid))
+    if (th.ptid != ptid)
       delete_thread (&th);
 
   /* We also need to clear any left over stale state for the
@@ -1308,7 +1309,6 @@ follow_exec (ptid_t ptid, const char *exec_file_target)
   update_breakpoints_after_exec ();
 
   /* What is this a.out's name?  */
-  process_ptid = ptid_t (pid);
   gdb_printf (_("%s is executing new program: %s\n"),
 	      target_pid_to_str (process_ptid).c_str (),
 	      exec_file_target);
diff --git a/gdb/remote.c b/gdb/remote.c
index 88668b2748e..442920f02ae 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -4584,34 +4584,29 @@ remote_target::update_thread_list ()
       /* CONTEXT now holds the current thread list on the remote
 	 target end.  Delete GDB-side threads no longer found on the
 	 target.  */
-      for (thread_info &tp : all_threads_safe ())
-	{
-	  if (tp.inf->process_target () != this)
-	    continue;
-
-	  if (!context.contains_thread (tp.ptid))
-	    {
-	      /* Do not remove the thread if it is the last thread in
-		 the inferior.  This situation happens when we have a
-		 pending exit process status to process.  Otherwise we
-		 may end up with a seemingly live inferior (i.e.  pid
-		 != 0) that has no threads.  */
-	      if (has_single_non_exited_thread (tp.inf))
-		continue;
+      for (thread_info &tp : all_threads_safe (this))
+	if (!context.contains_thread (tp.ptid))
+	  {
+	    /* Do not remove the thread if it is the last thread in
+	       the inferior.  This situation happens when we have a
+	       pending exit process status to process.  Otherwise we
+	       may end up with a seemingly live inferior (i.e.  pid
+	       != 0) that has no threads.  */
+	    if (has_single_non_exited_thread (tp.inf))
+	      continue;
 
-	      /* Do not remove the thread if we've requested to be
-		 notified of its exit.  For example, the thread may be
-		 displaced stepping, infrun will need to handle the
-		 exit event, and displaced stepping info is recorded
-		 in the thread object.  If we deleted the thread now,
-		 we'd lose that info.  */
-	      if ((tp.thread_options () & GDB_THREAD_OPTION_EXIT) != 0)
-		continue;
+	    /* Do not remove the thread if we've requested to be
+	       notified of its exit.  For example, the thread may be
+	       displaced stepping, infrun will need to handle the
+	       exit event, and displaced stepping info is recorded
+	       in the thread object.  If we deleted the thread now,
+	       we'd lose that info.  */
+	    if ((tp.thread_options () & GDB_THREAD_OPTION_EXIT) != 0)
+	      continue;
 
-	      /* Not found.  */
-	      delete_thread (&tp);
-	    }
-	}
+	    /* Not found.  */
+	    delete_thread (&tp);
+	  }
 
       /* Remove any unreported fork/vfork/clone child threads from
 	 CONTEXT so that we don't interfere with follow
diff --git a/gdb/thread-iter.h b/gdb/thread-iter.h
index 3c15ada1cbb..4e7e9ba9634 100644
--- a/gdb/thread-iter.h
+++ b/gdb/thread-iter.h
@@ -162,10 +162,11 @@ using all_non_exited_threads_iterator
 using inf_non_exited_threads_iterator
   = filtered_iterator<inf_threads_iterator, non_exited_thread_filter>;
 
-/* Iterate over all threads of all inferiors, safely.  */
+/* Iterate over all threads that are matched by the wrapped
+   iterator, safely.  */
 
-using all_threads_safe_iterator
-  = basic_safe_iterator<all_threads_iterator>;
+using all_matching_threads_safe_iterator
+  = basic_safe_iterator<all_matching_threads_iterator>;
 
 /* Iterate over all threads of an inferior, safely.  */
 
@@ -189,10 +190,11 @@ using inf_non_exited_threads_range
 using safe_inf_threads_range = iterator_range<safe_inf_threads_iterator>;
 
 /* A range adapter that makes it possible to iterate over all threads
-   with range-for "safely".  I.e., it is safe to delete the
-   currently-iterated thread.  */
+   that are matched by the given iterator with range-for "safely".  I.e.,
+   it is safe to delete the currently-iterated thread.  */
 
-using all_threads_safe_range = iterator_range<all_threads_safe_iterator>;
+using all_matching_threads_safe_range
+  = iterator_range<all_matching_threads_safe_iterator>;
 
 /* A range adapter that makes it possible to iterate over all threads
    that match a PTID filter with range-for.  */
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.