[v2] gdb: use correct target in notify_thread_exited()

Message ID 20260505075744.936344-1-markus.t.metzger@intel.com
State New
Headers
Series [v2] gdb: use correct target in notify_thread_exited() |

Commit Message

Metzger, Markus T May 5, 2026, 7:57 a.m. UTC
  clean_up_just_stopped_threads_fsms() may call notify_thread_exited() in
the context of a thread from a different target.

Switch to the thread we're notifying about to get the inferior and target
right.

CC: Simon Marchi <simark@simark.ca>
---
 gdb/thread.c | 3 +++
 1 file changed, 3 insertions(+)
  

Comments

Simon Marchi May 5, 2026, 4:20 p.m. UTC | #1
On 2026-05-05 03:57, Markus Metzger wrote:
> clean_up_just_stopped_threads_fsms() may call notify_thread_exited() in
> the context of a thread from a different target.
> 
> Switch to the thread we're notifying about to get the inferior and target
> right.
> 
> CC: Simon Marchi <simark@simark.ca>
> ---
>  gdb/thread.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/gdb/thread.c b/gdb/thread.c
> index c156f16377b..02ea62d524c 100644
> --- a/gdb/thread.c
> +++ b/gdb/thread.c
> @@ -203,6 +203,9 @@ notify_thread_exited (thread_info *t, std::optional<ULONGEST> exit_code,
>  {
>    if (!silent && print_thread_events)
>      {
> +      scoped_restore_current_thread restore_thread;
> +      switch_to_thread (t);

Let's use:

  /* Switch inferior before the target calls below.  */
  auto restore_inf = maybe_switch_inferior (t->inf);


> +
>        if (exit_code.has_value ())
>  	gdb_printf (_("[%s (id %s) exited with code %s]\n"),
>  		    target_pid_to_str (t->ptid).c_str (),

I checked if any callee of the observable cared whether THREAD is the
current thread, or if THREAD's inferior is the current inferior, and I
don't think so.  remove_threaded_breakpoints recurses into a bunch of
breakpoint functions so it's difficult to be sure, but at first glance
it seems fine.

With the above fixed:

Approved-By: Simon Marchi <simon.marchi@efficios.com>

Simon
  

Patch

diff --git a/gdb/thread.c b/gdb/thread.c
index c156f16377b..02ea62d524c 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -203,6 +203,9 @@  notify_thread_exited (thread_info *t, std::optional<ULONGEST> exit_code,
 {
   if (!silent && print_thread_events)
     {
+      scoped_restore_current_thread restore_thread;
+      switch_to_thread (t);
+
       if (exit_code.has_value ())
 	gdb_printf (_("[%s (id %s) exited with code %s]\n"),
 		    target_pid_to_str (t->ptid).c_str (),