From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id otEZDLc5r2rwgCMAWB0awg (envelope-from ) for ; Sat, 19 Sep 2026 21:41:11 -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=YcieBvj/; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 06EEA1E051; Sat, 19 Sep 2026 21:41:11 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-0.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,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=no autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.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 1B20E1E033 for ; Sat, 19 Sep 2026 21:41:10 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D082E4B9DB48 for ; Sun, 20 Sep 2026 01:41:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D082E4B9DB48 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=YcieBvj/ 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 5C8984BA9023 for ; Sun, 20 Sep 2026 01:40:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5C8984BA9023 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 5C8984BA9023 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=1789868441; cv=none; b=McRTkceaUxi6AV2n02VLtIPzbrEqLKhyeUceZh/1lydGFFJyIeqiHQzYhLh2KU4WFVolH5736wHZCTRAzU1ZJTIqnILvrRbKOxGe7qXTHwUdZCovl8l22qJLC9JjVzw3ZeYFs1rt+3g2dO50F2OdIioLF4U0/eOUvUUbx6tefSk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1789868441; c=relaxed/simple; bh=TDgRqHfh9qyLjKF6dFdeYoBmhs6M+1bVEDFqEckZnyg=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=nR+AqlqjgF8R9hNJeHcmLbaBJnlFsLaguetwLlSum2ZyT+y2uPnvH5KTVmpz6GqzQnfyNeKM1OdsDGWGnmO9RJ+9HckIJ/4hmWRyGahU+cdx9/00zU1u2ryMJ299SBMh00KsUFcNSuzX8GkQatQGd87XoEhjlqLCgheTHOkBGHg= 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=YcieBvj/ DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5C8984BA9023 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1789868440; 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=gh0zOy7oKLN4k8VmXMvlXOyrjZ3+aLa6M4kJ/pBKgaQ=; b=YcieBvj/JtQh4cMQt/vzJceNl09RXW0q8zaaE7kSzsYBdtzNCgfSGMgQRrdk7qZfZ7S8nq lyQ6Yp6mbkGLNQm3v16BdE5QE9tFko6A1oGrb6bD5Ci354sTfZCoehIqx678SwwVO1rHQM pmjSmd1RfFnSCE2nVaGWqiSms7XniF0= 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-557-Ng7fRFlePfy1FD_2xfTy4g-1; Sat, 19 Sep 2026 21:40:36 -0400 X-MC-Unique: Ng7fRFlePfy1FD_2xfTy4g-1 X-Mimecast-MFC-AGG-ID: Ng7fRFlePfy1FD_2xfTy4g_1789868436 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 4D2E91955F19; Sun, 20 Sep 2026 01:40:35 +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 63F6C214; Sun, 20 Sep 2026 01:40:34 +0000 (UTC) Date: Sat, 19 Sep 2026 18:40:31 -0700 From: Kevin Buettner To: gdb-patches@sourceware.org Cc: Klaus Gerlicher Subject: Re: [PATCH v4 1/1] gdb: remember previously selected thread per inferior Message-ID: <20260919184031.6aaf34d1@f44-mesa-1> In-Reply-To: <20260911081015.104335-2-klaus.gerlicher@intel.com> References: <20260911081015.104335-1-klaus.gerlicher@intel.com> <20260911081015.104335-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: G55wHPQ08nkQcDAv2nlQCoUEEzMBm7LAmf8f8UY2rtI_1789868436 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, This is much better! Just a few nits to fix... On Fri, 11 Sep 2026 08:10:15 +0000 Klaus Gerlicher 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 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