From patchwork Thu Nov 10 22:19:28 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 17394 Received: (qmail 61450 invoked by alias); 10 Nov 2016 22:20:14 -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 60884 invoked by uid 89); 10 Nov 2016 22:20:10 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.4 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS, T_FILL_THIS_FORM_SHORT autolearn=no version=3.3.2 spammy=gil, 5016, 501, 6 X-HELO: gproxy10-pub.mail.unifiedlayer.com Received: from gproxy10-pub.mail.unifiedlayer.com (HELO gproxy10-pub.mail.unifiedlayer.com) (69.89.20.226) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with SMTP; Thu, 10 Nov 2016 22:20:00 +0000 Received: (qmail 9368 invoked by uid 0); 10 Nov 2016 22:19:59 -0000 Received: from unknown (HELO CMOut01) (10.0.90.82) by gproxy10.mail.unifiedlayer.com with SMTP; 10 Nov 2016 22:19:59 -0000 Received: from box522.bluehost.com ([74.220.219.122]) by CMOut01 with id 6NKs1u00K2f2jeq01NKvmq; Thu, 10 Nov 2016 15:19:57 -0700 X-Authority-Analysis: v=2.1 cv=beT4Do/B c=1 sm=1 tr=0 a=GsOEXm/OWkKvwdLVJsfwcA==:117 a=GsOEXm/OWkKvwdLVJsfwcA==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=L24OOQBejmoA:10 a=zstS-IiYAAAA:8 a=7dGIayJqlgIqPDtBBf8A:9 a=4G6NA9xxw8l3yy4pmD5M:22 Received: from 174-16-143-211.hlrn.qwest.net ([174.16.143.211]:60120 helo=bapiya.Home) by box522.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.86_1) (envelope-from ) id 1c4xhJ-0002sd-Ng; Thu, 10 Nov 2016 15:19:53 -0700 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFA 01/20] Introduce gdbpy_enter Date: Thu, 10 Nov 2016 15:19:28 -0700 Message-Id: <1478816387-27064-2-git-send-email-tom@tromey.com> In-Reply-To: <1478816387-27064-1-git-send-email-tom@tromey.com> References: <1478816387-27064-1-git-send-email-tom@tromey.com> X-BWhitelist: no X-Exim-ID: 1c4xhJ-0002sd-Ng X-Source-Sender: 174-16-143-211.hlrn.qwest.net (bapiya.Home) [174.16.143.211]:60120 X-Source-Auth: tom+tromey.com X-Email-Count: 2 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTIyLmJsdWVob3N0LmNvbQ== This introduces gdbpy_enter, a class that can be used to acquire and release the Python GIL, and also set other Python-related globals used by gdb. ensure_python_env is rewritten in terms of this new class. 2016-11-10 Tom Tromey * python/python.c (gdbpy_enter): New constructor. (~gdbpy_enter): New destructor. (restore_python_env, ensure_python_env): Rewrite. * python/python-internal.h (gdbpy_enter): New class. --- gdb/ChangeLog | 7 +++++ gdb/python/python-internal.h | 23 +++++++++++++++ gdb/python/python.c | 69 ++++++++++++++++++++------------------------ 3 files changed, 62 insertions(+), 37 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index b670e69..460acf2 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,5 +1,12 @@ 2016-11-10 Tom Tromey + * python/python.c (gdbpy_enter): New constructor. + (~gdbpy_enter): New destructor. + (restore_python_env, ensure_python_env): Rewrite. + * python/python-internal.h (gdbpy_enter): New class. + +2016-11-10 Tom Tromey + * python/py-symbol.c (gdbpy_lookup_symbol): Use gdbpy_reference. 2016-11-10 Tom Tromey diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index f3213bb..2aca6ff 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -501,6 +501,29 @@ int gdbpy_initialize_unwind (void) struct cleanup *make_cleanup_py_decref (PyObject *py); struct cleanup *make_cleanup_py_xdecref (PyObject *py); +/* Restore global language and architecture and Python GIL state + when leaving the Python interpreter. */ +class gdbpy_enter +{ + public: + + gdbpy_enter (struct gdbarch *gdbarch, const struct language_defn *language); + + ~gdbpy_enter (); + + private: + + // Intentionally not defined. + gdbpy_enter (const gdbpy_enter &); + gdbpy_enter &operator= (const gdbpy_enter &); + + struct active_ext_lang_state *m_previous_active; + PyGILState_STATE m_state; + struct gdbarch *m_gdbarch; + const struct language_defn *m_language; + PyObject *m_error_type, *m_error_value, *m_error_traceback; +}; + struct cleanup *ensure_python_env (struct gdbarch *gdbarch, const struct language_defn *language); diff --git a/gdb/python/python.c b/gdb/python/python.c index 3d7b88c..78b4507 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -202,23 +202,28 @@ const struct extension_language_ops python_extension_ops = struct gdbarch *python_gdbarch; const struct language_defn *python_language; -/* Restore global language and architecture and Python GIL state - when leaving the Python interpreter. */ - -struct python_env +gdbpy_enter::gdbpy_enter (struct gdbarch *gdbarch, + const struct language_defn *language) +: m_gdbarch (python_gdbarch), + m_language (python_language) { - struct active_ext_lang_state *previous_active; - PyGILState_STATE state; - struct gdbarch *gdbarch; - const struct language_defn *language; - PyObject *error_type, *error_value, *error_traceback; -}; + /* We should not ever enter Python unless initialized. */ + if (!gdb_python_initialized) + error (_("Python not initialized")); -static void -restore_python_env (void *p) -{ - struct python_env *env = (struct python_env *)p; + m_previous_active = set_active_ext_lang (&extension_language_python); + m_state = PyGILState_Ensure (); + + python_gdbarch = gdbarch; + python_language = language; + + /* Save it and ensure ! PyErr_Occurred () afterwards. */ + PyErr_Fetch (&m_error_type, &m_error_value, &m_error_traceback); +} + +gdbpy_enter::~gdbpy_enter () +{ /* Leftover Python error is forbidden by Python Exception Handling. */ if (PyErr_Occurred ()) { @@ -227,15 +232,21 @@ restore_python_env (void *p) warning (_("internal error: Unhandled Python exception")); } - PyErr_Restore (env->error_type, env->error_value, env->error_traceback); + PyErr_Restore (m_error_type, m_error_value, m_error_traceback); - PyGILState_Release (env->state); - python_gdbarch = env->gdbarch; - python_language = env->language; + PyGILState_Release (m_state); + python_gdbarch = m_gdbarch; + python_language = m_language; - restore_active_ext_lang (env->previous_active); + restore_active_ext_lang (m_previous_active); +} + +static void +restore_python_env (void *p) +{ + gdbpy_enter *env = (gdbpy_enter *) p; - xfree (env); + delete env; } /* Called before entering the Python interpreter to install the @@ -248,23 +259,7 @@ struct cleanup * ensure_python_env (struct gdbarch *gdbarch, const struct language_defn *language) { - struct python_env *env = XNEW (struct python_env); - - /* We should not ever enter Python unless initialized. */ - if (!gdb_python_initialized) - error (_("Python not initialized")); - - env->previous_active = set_active_ext_lang (&extension_language_python); - - env->state = PyGILState_Ensure (); - env->gdbarch = python_gdbarch; - env->language = python_language; - - python_gdbarch = gdbarch; - python_language = language; - - /* Save it and ensure ! PyErr_Occurred () afterwards. */ - PyErr_Fetch (&env->error_type, &env->error_value, &env->error_traceback); + gdbpy_enter *env = new gdbpy_enter (gdbarch, language); return make_cleanup (restore_python_env, env); }