]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: use all_threads() in for_each_thread()
authorMarkus Metzger <markus.t.metzger@intel.com>
Mon, 11 May 2026 08:53:21 +0000 (08:53 +0000)
committerMarkus Metzger <markus.t.metzger@intel.com>
Fri, 15 May 2026 05:10:47 +0000 (05:10 +0000)
In for_each_thread(), we use all_threads_safe(), which allows deleting the
thread.  This it not needed in the cases where it is currently used and it
causes the iterator to get corrupted when threads are deleted in a nested
all_threads_safe() iteration.

One such scenario is as follows:
- remote debugging in non-stop mode
- one thread has hit a breakpoint
- one thread exited but GDB does not know about it, yet
- resume with 'continue -a'
- the continue command uses for_each_thread() to proceed threads individually
- the stopped thread starts an inline step-over (displaced stepping disabled)
- update_thread_list() learns about the exit and deletes the thread
- the safe iterator's m_next still points to the deleted thread

Approved-By: Tom Tromey <tom@tromey.com>
gdb/gdbthread.h
gdb/thread.c

index b3052b28d1abc6baea35c0d7b5f6a7dacdaff324..1bdbf621982938280f8f33b3610042c0f1c72a29 100644 (file)
@@ -796,7 +796,14 @@ void thread_change_ptid (process_stratum_target *targ,
 using for_each_thread_callback_ftype
   = gdb::function_view<void (thread_info *)>;
 
-/* Call CALLBACK once for each known thread.  */
+/* Call CALLBACK once for each known thread.
+
+   CALLBACK must not delete the thread.  To delete threads, use:
+
+     for (thread_info &t : all_threads_safe ())
+       if (some_condition ())
+        delete &t;
+*/
 
 extern void for_each_thread (for_each_thread_callback_ftype callback);
 
index 4f62ca280cdb1f527dc224a0c6232bc039e8fda3..110e58a70aaec2595bdbe702fea6c3dd1e9d6ca9 100644 (file)
@@ -605,7 +605,7 @@ find_thread_by_handle (gdb::array_view<const gdb_byte> handle,
 void
 for_each_thread (for_each_thread_callback_ftype callback)
 {
-  for (thread_info &tp : all_threads_safe ())
+  for (thread_info &tp : all_threads ())
     callback (&tp);
 }