]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: parameterize all_threads_safe for the target and ptid
authorTankut Baris Aktemur <tankut.baris.aktemur@intel.com>
Tue, 21 Apr 2026 07:11:01 +0000 (09:11 +0200)
committerTankut Baris Aktemur <tankut.baris.aktemur@intel.com>
Tue, 21 Apr 2026 07:11:01 +0000 (09:11 +0200)
'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 <simon.marchi@efficios.com>
gdb/gdbthread.h
gdb/infrun.c
gdb/remote.c
gdb/thread-iter.h

index 729dcf5ab0683c4b0c71a00870550f5cce8edf58..bfafcd61e8088404d8201c19590a6d30c27ee1e8 100644 (file)
@@ -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);
index b295956f8ea5ccc6e79b4d0792bfd2cd62aa4d99..6401df78e0a38cbcf41b101ca2993db9814790bf 100644 (file)
@@ -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);
index 88668b2748ee7ac933db5182d643bec2b731eb09..442920f02ae763562d940d25b9eace9403c3178e 100644 (file)
@@ -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
index 3c15ada1cbbba031c1558e58ca49502ca1757d07..4e7e9ba963435046f94e65850259682f72e220be 100644 (file)
@@ -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.  */