Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Eli Zaretskii <eliz@gnu.org>
To: Klaus Gerlicher <klaus.gerlicher@intel.com>
Cc: gdb-patches@sourceware.org, kevinb@redhat.com
Subject: Re: [PATCH v5 1/1] gdb: remember previously selected thread per inferior
Date: Fri, 02 Oct 2026 13:34:06 +0300	[thread overview]
Message-ID: <86ld8gtmqp.fsf@gnu.org> (raw)
In-Reply-To: <20261002071229.157788-1-klaus.gerlicher@intel.com> (message from Klaus Gerlicher on Fri, 2 Oct 2026 07:12:29 +0000)

> From: Klaus Gerlicher <klaus.gerlicher@intel.com>
> Date: Fri,  2 Oct 2026 07:12:29 +0000
> 
> From: "Gerlicher, Klaus" <klaus.gerlicher@intel.com>
> 
> Add support for remembering which thread was selected when switching
> between inferiors.  This feature can be controlled with a new setting
> 'set remember-threads-per-inferior' which is disabled by default.
> 
> Before this patch, GDB selected any non-exited thread when switching to
> another inferior.  Note in the below example that the last inferior
> command ends up in thread 2.1 even though we were previously in
> thread 2.2:
> 
>   (gdb) info threads
>     Id   Target Id
>     1.1  Thread 0x7ffff7d7b740 (LWP 572047) "inferior-switch"
>     1.2  Thread 0x7ffff7d7a640 (LWP 572050) "inferior-switch"
>   * 2.1  Thread 0x7ffff7d7b740 (LWP 572618) "inferior-switch"
>     2.2  Thread 0x7ffff7d7a640 (LWP 572619) "inferior-switch"
> 
>   (gdb) thread 2.2
>   [Switching to thread 2.2 (Thread 0x7ffff7d7a640 (LWP 572619))]
> 
>   (gdb) inferior 1
>   [Switching to inferior 1 [process 572047] (/bin/inferior-switch)]
>   [Switching to thread 1.1 (Thread 0x7ffff7d7b740 (LWP 572047))]
> 
>   (gdb) thread 1.2
>   [Switching to thread 1.2 (Thread 0x7ffff7d7a640 (LWP 572050))]
> 
>   (gdb) inferior 2
>   [Switching to inferior 2 [process 572618] (/bin/inferior-switch)]
>   [Switching to thread 2.1 (Thread 0x7ffff7d7b740 (LWP 572618))]
>   ^^^ Expected 2.2, got 2.1 (context lost)
> 
> With this patch, GDB now records which thread was current when switching
> away from an inferior, and automatically restores that thread when
> switching back (if it still exists):
> 
>   (gdb) set remember-threads-per-inferior on
> 
>   (gdb) info threads
>     Id   Target Id
>     1.1  Thread 0x7ffff7d7b740 (LWP 559826) "inferior-switch"
>     1.2  Thread 0x7ffff7d7a640 (LWP 559829) "inferior-switch"
>   * 2.1  Thread 0x7ffff7d7b740 (LWP 560308) "inferior-switch"
>     2.2  Thread 0x7ffff7d7a640 (LWP 560497) "inferior-switch"
> 
>   (gdb) thread 2.2
>   [Switching to thread 2.2 (Thread 0x7ffff7d7a640 (LWP 560497))]
> 
>   (gdb) inferior 1
>   [Switching to inferior 1 [process 559826] (/bin/inferior-switch)]
>   [Switching to thread 1.1 (Thread 0x7ffff7d7b740 (LWP 559826))]
> 
>   (gdb) thread 1.2
>   [Switching to thread 1.2 (Thread 0x7ffff7d7a640 (LWP 559829))]
> 
>   (gdb) inferior 2
>   [Switching to inferior 2 [process 560308] (/bin/inferior-switch)]
>   [Switching to thread 2.2 (Thread 0x7ffff7d7a640 (LWP 560497))]
>   ^^^ Now correctly restores thread 2.2
> 
>   (gdb) inferior 1
>   [Switching to inferior 1 [process 559826] (/bin/inferior-switch)]
>   [Switching to thread 1.2 (Thread 0x7ffff7d7a640 (LWP 559829))]
>   ^^^ And thread 1.2 is also remembered
> 
> The feature improves the debugging experience when working with multiple
> inferiors by maintaining context.  Users who prefer the original behavior
> (or find the new behavior surprising) can keep the feature disabled.
> 
> The feature is disabled by default to maintain backward compatibility and
> avoid surprising users who expect deterministic "first thread" selection.
> 
> Implementation Details:
> 
> The implementation stores a thread reference in each inferior structure
> and uses three mechanisms to keep it updated:
> 
> 1. The user_selected_context_changed observer handles explicit user
>    commands (thread, inferior) that switch the current thread.
> 
> 2. A direct call in fetch_inferior_event handles event-driven thread
>    switches that occur during execution (e.g., breakpoints, signals,
>    schedule-multiple mode).
> 
> 3. A set_func on the setting captures the current inferior's thread
>    immediately when the feature is enabled, so turning it on doesn't
>    forget the thread you're already on.
> 
> The first two are necessary because the user_selected_context_changed
> observer only fires for explicit user commands, not for automatic
> switches triggered by events.  Without the fetch_inferior_event save
> point, automatic switches would not be recorded and the feature would
> break in async scenarios.
> 
> When switching to an inferior, the stored thread is restored if it still
> exists; otherwise, any available thread is selected as before.  The
> feature respects the setting at both save and restore points to ensure
> correct behavior when toggling the feature on/off.
> 
> Testing:
> 
> gdb.base/inferior-switch.exp covers both paths: explicit switches
> (user runs "thread"/"inferior") and implicit ones (breakpoints hit
> under schedule-multiple), plus toggling the setting on and off mid
> session.  gdb.mi/user-selected-context-sync.exp is adjusted to reset
> to a known thread before each sub-test, since it no longer relies on
> GDB always picking the same "first" thread.
> ---
> Kevin, thanks for the review, here it is addressed as V5.
> 
>  gdb/NEWS                                      |   8 +
>  gdb/doc/gdb.texinfo                           |  20 ++
>  gdb/gdbthread.h                               |   4 +
>  gdb/inferior.c                                |  76 ++++++-
>  gdb/inferior.h                                |  14 ++
>  gdb/infrun.c                                  |  12 +-
>  gdb/testsuite/gdb.base/inferior-switch.c      |  42 ++++
>  gdb/testsuite/gdb.base/inferior-switch.exp    | 187 ++++++++++++++++++
>  .../gdb.mi/user-selected-context-sync.exp     |  14 ++
>  gdb/thread.c                                  |   8 +
>  10 files changed, 382 insertions(+), 3 deletions(-)
>  create mode 100644 gdb/testsuite/gdb.base/inferior-switch.c
>  create mode 100644 gdb/testsuite/gdb.base/inferior-switch.exp

Thanks, the documentation parts are okay.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>

  reply	other threads:[~2026-10-02 10:34 UTC|newest]

Thread overview: 6+ 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
2026-10-02  7:12     ` [PATCH v5 " Klaus Gerlicher
2026-10-02 10:34       ` Eli Zaretskii [this message]
2026-10-21  6:58       ` [PING PATCH " Gerlicher, Klaus

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=86ld8gtmqp.fsf@gnu.org \
    --to=eliz@gnu.org \
    --cc=gdb-patches@sourceware.org \
    --cc=kevinb@redhat.com \
    --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