From patchwork Sat Sep 15 04:07:32 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 29384 Received: (qmail 93704 invoked by alias); 15 Sep 2018 04:07:39 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 93272 invoked by uid 89); 15 Sep 2018 04:07:39 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=gdb_assert, solves X-HELO: gateway23.websitewelcome.com Received: from gateway23.websitewelcome.com (HELO gateway23.websitewelcome.com) (192.185.49.219) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 15 Sep 2018 04:07:37 +0000 Received: from cm14.websitewelcome.com (cm14.websitewelcome.com [100.42.49.7]) by gateway23.websitewelcome.com (Postfix) with ESMTP id 4D4494B69 for ; Fri, 14 Sep 2018 23:07:36 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id 11rsgnxADkBj611rsgyZsk; Fri, 14 Sep 2018 23:07:36 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:MIME-Version :Content-Type:Content-Transfer-Encoding:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=ulF76uNNKEfig/Z48KM/lkl4PbU36M6zvRd1lXvKJKQ=; b=EfiImHG3L7CfCeIO7raNbM8+b9 IxXUr9ZNfdANFoptgwXTTBLX+Y3N0jz+Npt2uMC+ZlqrOkgWFq27zsEqlcX479LLmGTVr7ElUyONN PC1OyvtyDrp0OZx17jGyWYy1+; Received: from 97-122-190-66.hlrn.qwest.net ([97.122.190.66]:41184 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.91) (envelope-from ) id 1g11rr-001AQt-S7; Fri, 14 Sep 2018 23:07:36 -0500 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFC] Release the GIL while running a gdb command or expression Date: Fri, 14 Sep 2018 22:07:32 -0600 Message-Id: <20180915040732.6718-1-tom@tromey.com> PR python/23615 points out that gdb.execute_gdb_command does not release the Python GIL. This means that, while the gdb command is running, other Python threads do not run. This patch solves the problem by introducing a new RAII class that can be used to temporarily release and then re-acquire the GIL, then puts this into the appropriate places in execute_gdb_command and gdbpy_parse_and_eval. The main issue with this patch is that I could not think of a non-racy way to test it. Any ideas? gdb/ChangeLog 2018-09-07 Tom Tromey PR python/23615: * python/python.c (execute_gdb_command): Use gdbpy_allow_threads. (gdbpy_parse_and_eval): Likewise. * python/python-internal.h (gdbpy_allow_threads): New class. --- gdb/ChangeLog | 7 +++++++ gdb/python/python-internal.h | 25 +++++++++++++++++++++++++ gdb/python/python.c | 3 +++ 3 files changed, 35 insertions(+) diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 785ad171511..9041fbeab3b 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -647,6 +647,31 @@ class gdbpy_enter_varobj : public gdbpy_enter }; +/* The opposite of gdb_enter: this releases the GIL around a region, + allowing other Python threads to run. No Python APIs may be used + while this is active. */ +class gdbpy_allow_threads +{ +public: + + gdbpy_allow_threads () + : m_save (PyEval_SaveThread ()) + { + gdb_assert (m_save != nullptr); + } + + ~gdbpy_allow_threads () + { + PyEval_RestoreThread (m_save); + } + + DISABLE_COPY_AND_ASSIGN (gdbpy_allow_threads); + +private: + + PyThreadState *m_save; +}; + extern struct gdbarch *python_gdbarch; extern const struct language_defn *python_language; diff --git a/gdb/python/python.c b/gdb/python/python.c index e89c90f8d9f..ca0c7478e91 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -586,6 +586,8 @@ execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw) TRY { + gdbpy_allow_threads allow_threads; + struct interp *interp; std::string arg_copy = arg; @@ -934,6 +936,7 @@ gdbpy_parse_and_eval (PyObject *self, PyObject *args) TRY { + gdbpy_allow_threads allow_threads; result = parse_and_eval (expr_str); } CATCH (except, RETURN_MASK_ALL)