From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id E9T/DaVpoGrw6TcAWB0awg (envelope-from ) for ; Tue, 08 Sep 2026 16:01:41 -0400 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=YDIqag0f; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 1B9261E09E; Tue, 08 Sep 2026 16:01:41 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-3.1 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,RCVD_IN_SBL_CSS 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 E50151E033 for ; Tue, 08 Sep 2026 16:01:39 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A58E24BB1C32 for ; Tue, 8 Sep 2026 20:01:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A58E24BB1C32 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=YDIqag0f Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 317534B99F6A for ; Tue, 8 Sep 2026 20:01:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 317534B99F6A Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 317534B99F6A Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1788897672; cv=none; b=RxuedPRkIN250Gt1TlH5uL83p6tf8VBTATQ/sNc4KADZM2RerilagIP4uoxlXWePg6AO+fOirqYBq5u2hlUHHxdPplnZj40O/RRHscNTzhyskVuCRXJN5cyBpz2+zdxCuQVO5DqDnhu3v8/o0tr5QtObByJyG68UMfnyQ4v/gsE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1788897672; c=relaxed/simple; bh=vNOTK9PPy3Q82JPfaf2iZt6AEwCLa119Pi4g2geGdWs=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=gY+YBJDziC5hOro35QnCPvRbkcJVhLzVGEeKHXp2v6ajOOIJUw5vSJRf/8IK/G1roJ/neQYb43faMg2pEdChnAZBNzHM6V+M50YsNISNkScQIqwD2saMvU61G2NNsiF2TXifamMvbrpBlyxRuZFeomyJFKx71ebFLnKGesWSqPo= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=YDIqag0f DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 317534B99F6A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1788897671; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/E29PQsKYQXTdhZEcb9cy57T4nISgoMZNCgZPG/vx1c=; b=YDIqag0fivVb48hVIsoOFkjuweAmyW4pF312uypQfVkZG0UerpMiRV3jX7HtfCmoQQU9t1 QW2bjOui+ksJqttgxk0HyNr6HcbLSZMzQ0FQKmmXhEL0UIjXZY1Ef8dRQ9w1RCSonRKpG3 RjqFYrzLfQ3TMqruuhrlU4n/nTv27AU= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-211-WjqF0N6tNy-Vxox_5NbJtA-1; Tue, 08 Sep 2026 16:01:08 -0400 X-MC-Unique: WjqF0N6tNy-Vxox_5NbJtA-1 X-Mimecast-MFC-AGG-ID: WjqF0N6tNy-Vxox_5NbJtA_1788897667 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 89BB11954239; Tue, 8 Sep 2026 20:01:07 +0000 (UTC) Received: from f44-mesa-1 (unknown [10.22.80.33]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C4BBB5B1; Tue, 8 Sep 2026 20:01:05 +0000 (UTC) Date: Tue, 8 Sep 2026 13:01:02 -0700 From: Kevin Buettner To: Klaus Gerlicher Cc: gdb-patches@sourceware.org, aburgess@redhat.com Subject: Re: [PATCH v3 1/1] gdb: remember previously selected thread per inferior Message-ID: <20260908130102.7ccb3698@f44-mesa-1> In-Reply-To: <20260908133426.444197-2-klaus.gerlicher@intel.com> References: <20260908133426.444197-1-klaus.gerlicher@intel.com> <20260908133426.444197-2-klaus.gerlicher@intel.com> Organization: Red Hat MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: YHOd1qJCl04pevnlAkrK15JcVKxfsXP7Qkhhd7p8Ds4_1788897667 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit 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 Hi Klaus, I had my AI reviewer look this over. It found some problems. In addition to needing a rebase w/ resultant build problems fixed, the test case needs some rework. I'll point out what it found below... On Tue, 8 Sep 2026 13:34:26 +0000 Klaus Gerlicher wrote: [...] > diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo > index 09080ccdad9..d4a59ff6445 100644 > --- a/gdb/doc/gdb.texinfo > +++ b/gdb/doc/gdb.texinfo > @@ -3518,6 +3518,26 @@ To switch focus between inferiors, use the @code{inferior} command: > Make inferior number @var{infno} the current inferior. The argument > @var{infno} is the inferior number assigned by @value{GDBN}, as shown > in the first field of the @samp{info inferiors} display. > + > +By default, when switching to an inferior, @value{GDBN} selects the first > +available thread. Optionally, @value{GDBN} can remember and restore the > +thread that was previously selected in that inferior (if it still exists). > +This behavior can be controlled with the @code{set remember-threads-per-inferior} > +command, described below. > +@end table > + > +@cindex remember threads per inferior > +@kindex set remember-threads-per-inferior > +@kindex show remember-threads-per-inferior > +@table @code > +@item set remember-threads-per-inferior on|off > +Control whether @value{GDBN} remembers the selected thread for each inferior. > +When enabled, switching to a previously used inferior will restore the thread > +that was active when you switched away. When disabled (the default), I believe that Eli asked for the word "selected" to be used instead of "active" here. [...] > diff --git a/gdb/inferior.c b/gdb/inferior.c > index 8c619ffec5a..9d8ea91f29e 100644 > --- a/gdb/inferior.c > +++ b/gdb/inferior.c > @@ -46,6 +46,9 @@ static int highest_inferior_num; > /* See inferior.h. */ > bool print_inferior_events = true; > > +/* Control whether to remember selected threads per inferior. */ > +bool remember_inferior_threads = false; > + > /* The Current Inferior. This is a strong reference. I.e., whenever > an inferior is the current inferior, its refcount is > incremented. */ > @@ -508,6 +511,32 @@ inferior_pid_to_str (int pid) > > /* See inferior.h. */ > > +void > +save_inferior_last_thread () > +{ > + if (has_current_thread ()) > + current_inferior ()->last_user_thread > + = thread_info_ref::new_reference (inferior_thread ()); > + else > + current_inferior ()->last_user_thread = nullptr; > +} > + > +/* Observer callback for explicit user-initiated thread switches. > + This captures switches from commands like "thread X" or "inferior Y". > + Note: This does NOT fire for automatic event-driven switches (e.g., > + breakpoint hits in schedule-multiple mode). Those are handled by a > + direct call in fetch_inferior_event. Both mechanisms are necessary > + to capture all thread switch scenarios. */ > + > +static void > +inferiors_on_user_selected_context_changed (user_selected_what selection) > +{ > + if (remember_inferior_threads && (selection & USER_SELECTED_THREAD)) > + save_inferior_last_thread (); > +} > + > +/* See inferior.h. */ > + > void > print_selected_inferior (struct ui_out *uiout) > { > @@ -771,8 +800,23 @@ inferior_command (const char *args, int from_tty) > { > if (inf != current_inferior ()) > { > - thread_info *tp = any_non_exited_thread_of_inferior (inf); > - if (tp == NULL) > + thread_info *tp = nullptr; > + > + if (remember_inferior_threads) > + { > + thread_info *stored_tp = inf->last_user_thread.get (); > + > + /* Fallback to selecting any non-exited thread of > + inferior. */ > + tp = (stored_tp == nullptr > + || stored_tp->state == THREAD_EXITED) thread_info::state is now a member function. That line should now be: || stored_tp->state () == THREAD_EXITED) > + ? any_non_exited_thread_of_inferior (inf) > + : stored_tp; > + } > + else > + tp = any_non_exited_thread_of_inferior (inf); > + > + if (tp == nullptr) > error (_("Inferior has no threads.")); > > switch_to_thread (tp); [...] > diff --git a/gdb/testsuite/gdb.base/inferior-switch.c b/gdb/testsuite/gdb.base/inferior-switch.c > new file mode 100644 > index 00000000000..3b94713ff43 > --- /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. > + This program is free software; you can redistribute it and/or modify > + it under the terms of the GNU General Public License as published by > + the Free Software Foundation; either version 3 of the License, or > + (at your option) any later version. > + This program is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + GNU General Public License for more details. > + You should have received a copy of the GNU General Public License > + along with this program. If not, see . */ > + > +#include > +#include > + > +static void * > +task (void *arg) > +{ > + volatile unsigned int duration = 0; > + > + int a = 1; /* worker thread break 1. */ > + > + sleep (duration); > + int b = 2; /* worker thread break 2. */ > + > + sleep (duration); > + return NULL; > +} > + > +int > +main (void) > +{ > + pthread_t th; > + > + alarm (30); > + > + pthread_create (&th, NULL, task, NULL); > + pthread_join (&th, NULL); I think this should be: pthread_join (th, NULL); [...] > diff --git a/gdb/testsuite/gdb.base/inferior-switch.exp b/gdb/testsuite/gdb.base/inferior-switch.exp > new file mode 100644 > index 00000000000..5cf7ee98fb1 > --- /dev/null > +++ b/gdb/testsuite/gdb.base/inferior-switch.exp [...] FYI, for this test, I'm seeing the failures below. I think this is likely due to the change in default behavior. FAIL: gdb.base/inferior-switch.exp: test_disabled_behavior: feature on again, restores previously selected thread 1.2 FAIL: gdb.base/inferior-switch.exp: test_explicit: again back to inferior 2 thread 2.2 FAIL: gdb.base/inferior-switch.exp: test_explicit: back to inferior 1 thread 1.2 FAIL: gdb.base/inferior-switch.exp: test_explicit: switch to inferior 2 thread 2.2 FAIL: gdb.base/inferior-switch.exp: test_implicit: switch inferior 1 stopped at 2nd BP FAIL: gdb.base/inferior-switch.exp: test_implicit: switch inferior 2 stopped at 2nd BP Kevin