* [PATCH] gdb: use all_threads() in for_each_thread()
@ 2026-05-12 6:32 Markus Metzger
2026-05-13 14:36 ` Tom Tromey
0 siblings, 1 reply; 2+ messages in thread
From: Markus Metzger @ 2026-05-12 6:32 UTC (permalink / raw)
To: gdb-patches
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
---
gdb/gdbthread.h | 9 ++++++++-
gdb/thread.c | 2 +-
2 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index b3052b28d1a..1bdbf621982 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -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);
diff --git a/gdb/thread.c b/gdb/thread.c
index 4f62ca280cd..110e58a70aa 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -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);
}
--
2.34.1
Intel Deutschland GmbH
Registered Address: Dornacher Strasse 1, 85622 Feldkirchen, Germany
Tel: +49 89 991 430, www.intel.de
Managing Directors: Harry Demas, Jeffrey Schneiderman, Yin Chong Sorrell
Chairperson of the Supervisory Board: Nicole Lau
Registered Seat: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] gdb: use all_threads() in for_each_thread()
2026-05-12 6:32 [PATCH] gdb: use all_threads() in for_each_thread() Markus Metzger
@ 2026-05-13 14:36 ` Tom Tromey
0 siblings, 0 replies; 2+ messages in thread
From: Tom Tromey @ 2026-05-13 14:36 UTC (permalink / raw)
To: Markus Metzger; +Cc: gdb-patches
>>>>> "Markus" == Markus Metzger <markus.t.metzger@intel.com> writes:
Markus> In for_each_thread(), we use all_threads_safe(), which allows deleting the
Markus> thread. This it not needed in the cases where it is currently used and it
Markus> causes the iterator to get corrupted when threads are deleted in a nested
Markus> all_threads_safe() iteration.
Thanks, this is ok.
Approved-By: Tom Tromey <tom@tromey.com>
Tom
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-05-13 14:37 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-05-12 6:32 [PATCH] gdb: use all_threads() in for_each_thread() Markus Metzger
2026-05-13 14:36 ` Tom Tromey
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox