From: Kevin Buettner <kevinb@redhat.com>
To: gdb-patches@sourceware.org
Cc: Klaus Gerlicher <klaus.gerlicher@intel.com>
Subject: Re: [PATCH v4 1/1] gdb: remember previously selected thread per inferior
Date: Sat, 19 Sep 2026 18:40:31 -0700 [thread overview]
Message-ID: <20260919184031.6aaf34d1@f44-mesa-1> (raw)
In-Reply-To: <20260911081015.104335-2-klaus.gerlicher@intel.com>
Hi Klaus,
This is much better! Just a few nits to fix...
On Fri, 11 Sep 2026 08:10:15 +0000
Klaus Gerlicher <klaus.gerlicher@intel.com> wrote:
[...]
> @@ -1255,5 +1310,22 @@ Show printing of inferior events (such as inferior start and exit)."), NULL,
> show_print_inferior_events,
> &setprintlist, &showprintlist);
>
> + add_setshow_boolean_cmd ("remember-threads-per-inferior", no_class,
> + &remember_inferior_threads, _("\
> +Set whether GDB will cache the current thread in each inferior."), _("\
> +Show whether GDB will cache the current thread in each inferior."), _("\
> +When on, GDB records which thread was current when switching away from\n\
> +an inferior, and automatically restores that thread when switching back\n\
> +to that inferior (if the thread still exists). When off, GDB selects\n\
> +the first available thread."),
> + set_remember_inferior_threads,
> + NULL,
> + &setlist, &showlist);
Please change the set/show help strings to use "remember" instead of
"cache".
> +
> create_internalvar_type_lazy ("_inferior", &inferior_funcs, NULL);
> +
> + /* Observe user_selected_context_changed to store the current user
> + thread. */
> + gdb::observers::user_selected_context_changed.attach (
> + inferiors_on_user_selected_context_changed, "inferiors");
> }
> diff --git a/gdb/inferior.h b/gdb/inferior.h
> index accd36740c3..b2bfa1a4029 100644
> --- a/gdb/inferior.h
> +++ b/gdb/inferior.h
> @@ -276,6 +276,10 @@ extern int stopped_by_random_signal;
> `set print inferior-events'. */
> extern bool print_inferior_events;
>
> +/* Control whether GDB remembers the selected thread per inferior, set with
> + `set remember-threads-per-inferior'. */
> +extern bool remember_inferior_threads;
> +
> /* Anything but NO_STOP_QUIETLY means we expect a trap and the caller
> will handle it themselves. STOP_QUIETLY is used when running in
> the shell before the child program has been exec'd and when running
> @@ -696,6 +700,10 @@ class inferior : public refcounted_object,
> /* Per inferior data-pointers required by other GDB modules. */
> registry<inferior> registry_fields;
>
> + /* The last thread that was current when inferior was switched away
> + from. */
> + thread_info_ref last_user_thread;
> +
> private:
>
> /* Unpush TARGET and assert that it worked. */
> @@ -902,6 +910,12 @@ extern void print_selected_inferior (struct ui_out *uiout);
> extern void switch_to_inferior_and_push_target
> (inferior *new_inf, bool no_connection, inferior *org_inf);
>
> +/* Record the current inferior's selected thread, so it can be
> + restored later. GDB will select the recorded thread, if alive,
> + instead of any thread from the current inferior. */
> +
> +extern void save_inferior_last_thread ();
> +
> /* Return true if ID is a valid global inferior number. */
>
> inline bool
> diff --git a/gdb/infrun.c b/gdb/infrun.c
> index b9618fb6422..caff2e97aee 100644
> --- a/gdb/infrun.c
> +++ b/gdb/infrun.c
> @@ -4886,7 +4886,17 @@ fetch_inferior_event ()
> if (!non_stop
> && cmd_done
> && ecs.ws.kind () != TARGET_WAITKIND_NO_RESUMED)
> - restore_thread.dont_restore ();
> + {
> + restore_thread.dont_restore ();
> +
> + /* Save current thread for remember-threads-per-inferior feature.
> + This handles automatic event-driven thread switches (breakpoints,
> + signals, schedule-multiple execution) which don't trigger the
> + user_selected_context_changed observer. The observer only fires
> + for explicit user commands like "thread X" or "inferior Y". */
> + if (remember_inferior_threads)
> + save_inferior_last_thread ();
> + }
> }
> }
>
> diff --git a/gdb/testsuite/gdb.base/inferior-switch.c b/gdb/testsuite/gdb.base/inferior-switch.c
> new file mode 100644
> index 00000000000..107090d003c
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/inferior-switch.c
> @@ -0,0 +1,42 @@
> +/* This testcase is part of GDB, the GNU debugger.
> + Copyright 2024 Free Software Foundation, Inc.
Update copyright dates to 2024-2026.
[...]
> diff --git a/gdb/testsuite/gdb.base/inferior-switch.exp b/gdb/testsuite/gdb.base/inferior-switch.exp
> new file mode 100644
> index 00000000000..b118059638d
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/inferior-switch.exp
> @@ -0,0 +1,187 @@
> +# Copyright 2024 Free Software Foundation, Inc.
Likewise.
With those nits fixed:
Approved-By: Kevin Buettner <kevinb@redhat.com>
prev parent reply other threads:[~2026-09-20 1:41 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-11 8:10 [PATCH v4 0/1] " Klaus Gerlicher
2026-09-11 8:10 ` [PATCH v4 1/1] " Klaus Gerlicher
2026-09-20 1:40 ` Kevin Buettner [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260919184031.6aaf34d1@f44-mesa-1 \
--to=kevinb@redhat.com \
--cc=gdb-patches@sourceware.org \
--cc=klaus.gerlicher@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox