From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id /A/lJMWIv2oY9CgAWB0awg (envelope-from ) for ; Fri, 02 Oct 2026 06:34:45 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=gnu.org header.i=@gnu.org header.a=rsa-sha256 header.s=fencepost-gnu-org header.b=ciF9uI0o; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 6F9271E050; Fri, 02 Oct 2026 06:34:45 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-6.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 752B21E04E for ; Fri, 02 Oct 2026 06:34:43 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8D33F4BB1C0D for ; Fri, 2 Oct 2026 10:34:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8D33F4BB1C0D Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gnu.org header.i=@gnu.org header.a=rsa-sha256 header.s=fencepost-gnu-org header.b=ciF9uI0o Received: from eggs.gnu.org (eggs.gnu.org [IPv6:2001:470:142:3::10]) by sourceware.org (Postfix) with ESMTPS id DB7314BA79B3 for ; Fri, 2 Oct 2026 10:34:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DB7314BA79B3 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gnu.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gnu.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org DB7314BA79B3 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2001:470:142:3::10 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790937251; cv=none; b=GMRk4H4GP2Y7L2WqeHKEGJPeMEbzJZlyUDe8kiEVqAc1P8NO6gTJ0JMjAItNsWrBOp8+cHIHjN/7P6lRaVlp1pqyzacVHC/huDSkJu0BT+IGrjwEBx4ITJunCzXb9OmXofDHbcqbLZXqUcP7D7HL9Eg0gIhMjxak60s9NCtvKQk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790937251; c=relaxed/simple; bh=aymWGF4vGOYQPR9hAv6E8GlFSw/UfWOXWSQb4se6dYQ=; h=DKIM-Signature:Date:Message-Id:From:To:Subject; b=Yi0EAJ85nKdqIKzk16OwDO8D8r+1kKVAFwVwkCijBZvBtibi2K780Ae0k4Pe0GgIFOuGycq8s3zuLeMvRQiQymSv2HnlFYklG/X9Bu549f4jfFZ1hijLuDQc37djNCRbC76ExYYSStnnKBC7WqTQx4K9grPA9V0g/1UVPzmo588= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gnu.org header.i=@gnu.org header.a=rsa-sha256 header.s=fencepost-gnu-org header.b=ciF9uI0o DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DB7314BA79B3 Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xCaaf-0004sA-6z; Fri, 02 Oct 2026 06:34:09 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date: mime-version; bh=ANeryjKAMLommTDxOo+eqjw8vu6qzSuKjDRyoNpx3gc=; b=ciF9uI0oCiVZ L4oO2dRQwaoNQoPq2OjM75thI3JBY1tdwPBJz7i2rYWteDBdJn1l1f52ML19fgHThoKVNHgDw/5KF Yk0hGCgbSyTUxl3eK0z6mvsJNqIFS7ea0XfDQA0PpzxbiZOmgfpG3tzYa71+tXAcO3WgrX1peWdNu Ha+hJTHCJvEteLF8MeTaU7L0J0CPpfKwoAVM4s2yvkInz/edYBrS2T5MBHiyvqibTx+Y8+U/tPuVU yyW/d1LxTavNgsmcn7zKpAEsu+Hzyb+jCM4wkmxINSD4mMHzKPUh6Yrxvi4QpEezvfmIcLVOZDExb Ct4s/Ks+JJGVgKq1L6zVQQ==; Date: Fri, 02 Oct 2026 13:34:06 +0300 Message-Id: <86ld8gtmqp.fsf@gnu.org> From: Eli Zaretskii To: Klaus Gerlicher Cc: gdb-patches@sourceware.org, kevinb@redhat.com In-Reply-To: <20261002071229.157788-1-klaus.gerlicher@intel.com> (message from Klaus Gerlicher on Fri, 2 Oct 2026 07:12:29 +0000) Subject: Re: [PATCH v5 1/1] gdb: remember previously selected thread per inferior References: <20260919184031.6aaf34d1@f44-mesa-1> <20261002071229.157788-1-klaus.gerlicher@intel.com> X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org > From: Klaus Gerlicher > Date: Fri, 2 Oct 2026 07:12:29 +0000 > > From: "Gerlicher, Klaus" > > 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