From patchwork Thu Dec 7 11:02:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandra Petlanova Hajkova X-Patchwork-Id: 81662 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 1F115385DC2F for ; Thu, 7 Dec 2023 11:03:05 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 4EC7B3858C30 for ; Thu, 7 Dec 2023 11:02:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4EC7B3858C30 Authentication-Results: sourceware.org; dmarc=pass (p=none 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 4EC7B3858C30 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701946972; cv=none; b=QInS9lNtYu3Nl5TfArtcx2WTfUE8jU8RZKB2cw22bswkKCzhOcLjsNXBY7sLBCLuCH3l7F/sYtNQUywmymgIxyr1/GSTIW9huaRlw4av/m/M3PTCaGmWX+6tNyuHP9Sd0qg4FKRk1sDThzaM4D898Vn5ZFSyMwOb0I8cBAkyl4o= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701946972; c=relaxed/simple; bh=T7BAT2zw8/mlMzRDNxJXUno2QMlf7VcdETtW4cvF/bU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=MGnCTWRMJGhzOPKaT0U61HzbMLi6zexb/vSVydvd8sNlil0DxHTEBFIWUhWuHiobaJyfR5tvSwn9pLAkpgUVKyzcQPkpWrHhZ3cIDmLlcIQZW6QY3TocHdsCG3VOMvh2abgxwILFfIpnaQkvdNDgWz8BDJJdyqfyvhV8q5TWr4o= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701946965; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=mLuT2Ag5dF1sJmtAjLWCnXXHKkZ8VHpfOi9F5lH20vk=; b=GFaoEl5iNrvHR35tlXE4Ezr2fPyaPXUTjPtYeuoK1k2oZ4V3n6fhmj/w2akcg8B5KqpXXc ADyE4GN6htkepeZiLkSGMOXWWbAh66hrwhnHllxIxBVWN8Z00oU0eBXFCvdfFY10c2k9b2 0+iq/mSmcyk6S5W7MLAt4ngfIRdX8M0= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-582-gZ7fvDX3NIm-Fu5QrKVvPw-1; Thu, 07 Dec 2023 06:02:43 -0500 X-MC-Unique: gZ7fvDX3NIm-Fu5QrKVvPw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8596884A2E4 for ; Thu, 7 Dec 2023 11:02:43 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.242.11]) by smtp.corp.redhat.com (Postfix) with ESMTPS id F22A91C060AF for ; Thu, 7 Dec 2023 11:02:42 +0000 (UTC) From: =?utf-8?q?Alexandra_H=C3=A1jkov=C3=A1?= To: gdb-patches@sourceware.org Subject: [PATCH 1/2] gdb: move gdbpy_gil into python-internal.h Date: Thu, 7 Dec 2023 12:02:39 +0100 Message-ID: <20231207110240.1853760-1-ahajkova@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Move gdbpy_gil class into python-internal.h, the next commit wants to make use of this class from a file other than python.c. --- gdb/python/python-internal.h | 24 ++++++++++++++++++++++++ gdb/python/python.c | 24 ------------------------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 86eb5436f2a..14e15574685 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -772,6 +772,30 @@ class gdbpy_allow_threads PyThreadState *m_save; }; +/* A helper class to save and restore the GIL, but without touching + the other globals that are handled by gdbpy_enter. */ + +class gdbpy_gil +{ +public: + + gdbpy_gil () + : m_state (PyGILState_Ensure ()) + { + } + + ~gdbpy_gil () + { + PyGILState_Release (m_state); + } + + DISABLE_COPY_AND_ASSIGN (gdbpy_gil); + +private: + + PyGILState_STATE m_state; +}; + /* Use this after a TRY_EXCEPT to throw the appropriate Python exception. */ #define GDB_PY_HANDLE_EXCEPTION(Exception) \ diff --git a/gdb/python/python.c b/gdb/python/python.c index 8a36673a3e4..f9df57b9fd5 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -264,30 +264,6 @@ gdbpy_enter::finalize () python_gdbarch = current_inferior ()->arch (); } -/* A helper class to save and restore the GIL, but without touching - the other globals that are handled by gdbpy_enter. */ - -class gdbpy_gil -{ -public: - - gdbpy_gil () - : m_state (PyGILState_Ensure ()) - { - } - - ~gdbpy_gil () - { - PyGILState_Release (m_state); - } - - DISABLE_COPY_AND_ASSIGN (gdbpy_gil); - -private: - - PyGILState_STATE m_state; -}; - /* Set the quit flag. */ static void From patchwork Thu Dec 7 11:02:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandra Petlanova Hajkova X-Patchwork-Id: 81661 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 2A85B385DC0B for ; Thu, 7 Dec 2023 11:03:04 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 250BB385840A for ; Thu, 7 Dec 2023 11:02:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 250BB385840A Authentication-Results: sourceware.org; dmarc=pass (p=none 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 250BB385840A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701946971; cv=none; b=vBf0sVzyAJ+umvmnPDJ97ewqA4HRWCX4do62Vy+1nvPz6v8qQdb2uXBn5nQAOtuJwb5piyXFZMrBK9ftdAsiIJa5L2419cG3znFE5F+lJPVyaKvs4qbI/qz8K4JxQJFb4kSIHGPzD4zTbdshpsZlFlUEyvOg6TgF0lI5/XNCwXw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701946971; c=relaxed/simple; bh=MwHc7ebsIUTMSgpmuzE/Q23kUukjz5+rwuQvz7+WcUs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Ttt7T0eEOlDcZhVBr4rOP2hk/A1E6AlGHPU/XYxnKM68RsGztdZxmJVkR0WE2GG/JJm7z0fNXDDyWXMh60oAzJfCD96lIbqt14sj/3oT4VDLZ/qKSZ7YtpX86rXkKOB5Br/xwVwB2hzx5m0tZHow7DP12ZjrYiVXARId1y5c04U= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701946965; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gItL2RIrverZJtq5PypLcDOdbTq1frEBabi6c8jW9VM=; b=Xj2WYjNcWPF2MKSbQxS4BR9XAO5Dza9PrBHdH/qwYMedWKAxpomqS9Me9lzjCYcshxZadc 7CPirmXfZIXGSyn+8wWJ2s1UuQX63aoIUtIxW2/dL2R1JtdQvd1aU8cUJw2/xGOp66L56/ aXpJVm56k7btwsNclwdLAIqJw9WptlE= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-198-LkRU2f5SNO2uW8C2287i_g-1; Thu, 07 Dec 2023 06:02:44 -0500 X-MC-Unique: LkRU2f5SNO2uW8C2287i_g-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6316584A2E2 for ; Thu, 7 Dec 2023 11:02:44 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.242.11]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DE7351C00043 for ; Thu, 7 Dec 2023 11:02:43 +0000 (UTC) From: =?utf-8?q?Alexandra_H=C3=A1jkov=C3=A1?= To: gdb-patches@sourceware.org Subject: [PATCH 2/2] gdb/python: avoid use of _PyOS_ReadlineTState Date: Thu, 7 Dec 2023 12:02:40 +0100 Message-ID: <20231207110240.1853760-2-ahajkova@redhat.com> In-Reply-To: <20231207110240.1853760-1-ahajkova@redhat.com> References: <20231207110240.1853760-1-ahajkova@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org From: Andrew Burgess In python/py-gdb-readline.c we make use of _PyOS_ReadlineTState, however, this variable is no longer public in Python 3.13, and so GDB no longer builds. We are making use of _PyOS_ReadlineTState in order to re-acquire the Python Global Interpreter Lock (GIL). The _PyOS_ReadlineTState variable is set in Python's outer readline code prior to calling the user (GDB) supplied readline callback function, which for us is gdbpy_readline_wrapper. The gdbpy_readline_wrapper function is called without the GIL held. Instead of using _PyOS_ReadlineTState, I propose that we switch to calling PyGILState_Ensure() and PyGILState_Release(). These functions will acquire the GIL based on the current thread. I think this should be sufficient; I can't imagine why we'd be running gdbpy_readline_wrapper on one thread on behalf of a different Python thread.... that would be unexpected I think. --- gdb/python/py-gdb-readline.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/gdb/python/py-gdb-readline.c b/gdb/python/py-gdb-readline.c index 124cec8055d..699d396dd19 100644 --- a/gdb/python/py-gdb-readline.c +++ b/gdb/python/py-gdb-readline.c @@ -56,13 +56,11 @@ gdbpy_readline_wrapper (FILE *sys_stdin, FILE *sys_stdout, if (except.reason == RETURN_QUIT) return NULL; - /* The thread state is nulled during gdbpy_readline_wrapper, - with the original value saved in the following undocumented - variable (see Python's Parser/myreadline.c and - Modules/readline.c). */ - PyEval_RestoreThread (_PyOS_ReadlineTState); + + /* This readline callback is called without the GIL held. */ + gdbpy_gil gil; + gdbpy_convert_exception (except); - PyEval_SaveThread (); return NULL; }