From patchwork Mon Apr 3 18:52:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Marchi X-Patchwork-Id: 67227 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C62F53853D3E for ; Mon, 3 Apr 2023 18:53:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C62F53853D3E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680547980; bh=67P3+44IkIiZDGJ7YxAsDtis192iRpfPMagkRyYchxI=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=Tcdb3hv1tI2sA8OxFKMbnpgR3My6R1yovaWZuod8IrSqJLY7ueia9mq4kJ+nu/Bli p0TCBSAxexhdsTE3+mpF5DXKJ17VNCn/Tv8heFtpUE5H751H51dqHOEA9NYii1dxtk VygEGUbLfyZJ8//0ch5PjJZAP1WlZiV9CTmwvYFo= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id E2FA63858C66 for ; Mon, 3 Apr 2023 18:52:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E2FA63858C66 Received: from localhost.localdomain (unknown [217.28.27.60]) (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 simark.ca (Postfix) with ESMTPSA id 6D2CF1E224; Mon, 3 Apr 2023 14:52:20 -0400 (EDT) To: gdb-patches@sourceware.org Cc: Simon Marchi , Pedro Alves Subject: [PATCH 4/7] gdb: add maybe_switch_inferior function Date: Mon, 3 Apr 2023 14:52:05 -0400 Message-Id: <20230403185208.197965-5-simon.marchi@efficios.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230403185208.197965-1-simon.marchi@efficios.com> References: <20230403185208.197965-1-simon.marchi@efficios.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1173.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, SPF_HELO_PASS, SPF_SOFTFAIL, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Simon Marchi via Gdb-patches From: Simon Marchi Reply-To: Simon Marchi Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" Add the maybe_switch_inferior function, which ensures that the given inferior is the current one. Return an instantiated scoped_restore_current_thread object only we actually needed to switch inferior. Returning a scoped_restore_current_thread requires it to be move-constructible, so give it a move constructor. Change-Id: I1231037102ed6166f2530399e8257ad937fb0569 Reviewed-By: Pedro Alves --- gdb/gdbthread.h | 2 ++ gdb/inferior.c | 15 +++++++++++++++ gdb/inferior.h | 7 +++++++ gdb/thread.c | 14 ++++++++++++++ 4 files changed, 38 insertions(+) diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h index 848daa94410a..731c5e159e9f 100644 --- a/gdb/gdbthread.h +++ b/gdb/gdbthread.h @@ -858,6 +858,8 @@ class scoped_restore_current_thread scoped_restore_current_thread (); ~scoped_restore_current_thread (); + scoped_restore_current_thread (scoped_restore_current_thread &&rhs); + DISABLE_COPY_AND_ASSIGN (scoped_restore_current_thread); /* Cancel restoring on scope exit. */ diff --git a/gdb/inferior.c b/gdb/inferior.c index a1e3c79d8a20..f6ed942c5053 100644 --- a/gdb/inferior.c +++ b/gdb/inferior.c @@ -672,6 +672,21 @@ switch_to_inferior_no_thread (inferior *inf) set_current_program_space (inf->pspace); } +/* See regcache.h. */ + +gdb::optional +maybe_switch_inferior (inferior *inf) +{ + gdb::optional maybe_restore_thread; + if (inf != current_inferior ()) + { + maybe_restore_thread.emplace (); + switch_to_inferior_no_thread (inf); + } + + return maybe_restore_thread; +} + static void inferior_command (const char *args, int from_tty) { diff --git a/gdb/inferior.h b/gdb/inferior.h index 72034cc4ffbc..ab981b7b4b27 100644 --- a/gdb/inferior.h +++ b/gdb/inferior.h @@ -340,6 +340,13 @@ extern void set_current_inferior (inferior *); selected. */ extern void switch_to_inferior_no_thread (inferior *inf); +/* Ensure INF is the current inferior. + + If the current inferior was changed, return an RAII object that will + restore the original current context. */ +extern gdb::optional maybe_switch_inferior + (inferior *inf); + /* Info about an inferior's target description. There's one of these for each inferior. */ diff --git a/gdb/thread.c b/gdb/thread.c index 25d97cd60727..506f8481e17b 100644 --- a/gdb/thread.c +++ b/gdb/thread.c @@ -1403,6 +1403,20 @@ scoped_restore_current_thread::scoped_restore_current_thread () } } +scoped_restore_current_thread::scoped_restore_current_thread + (scoped_restore_current_thread &&rhs) + : m_dont_restore (std::move (rhs.m_dont_restore)), + m_thread (std::move (rhs.m_thread)), + m_inf (std::move (rhs.m_inf)), + m_selected_frame_id (std::move (rhs.m_selected_frame_id)), + m_selected_frame_level (std::move (rhs.m_selected_frame_level)), + m_was_stopped (std::move (rhs.m_was_stopped)), + m_lang (std::move (rhs.m_lang)) +{ + /* Deactivate the rhs. */ + rhs.m_dont_restore = true; +} + /* See gdbthread.h. */ int