From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 7909 invoked by alias); 11 Jan 2012 22:22:11 -0000 Received: (qmail 7897 invoked by uid 22791); 11 Jan 2012 22:22:10 -0000 X-SWARE-Spam-Status: No, hits=-1.6 required=5.0 tests=AWL,BAYES_00,T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from router-304.cs.umd.edu (HELO bacon.cs.umd.edu) (128.8.127.145) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 11 Jan 2012 22:21:57 +0000 Received: from wireless-206-196-163-53.umd.edu (wireless-206-196-163-53.umd.edu [206.196.163.53]) (Authenticated sender: khooyp) by bacon.cs.umd.edu (Postfix) with ESMTPSA id 371EAB411F8; Wed, 11 Jan 2012 17:21:55 -0500 (EST) Subject: Re: Handle SIGINT in Python Mime-Version: 1.0 (Apple Message framework v1084) Content-Type: multipart/mixed; boundary=Apple-Mail-10-120964043 From: Khoo Yit Phang In-Reply-To: <1FE2845A-162C-48D7-A3D0-C0F2D6DFA09B@cs.umd.edu> Date: Wed, 11 Jan 2012 22:46:00 -0000 Cc: Tom Tromey , gdb-patches@sourceware.org Message-Id: <74929AB6-8260-4E42-B9E4-D533511D718F@cs.umd.edu> References: <1FE2845A-162C-48D7-A3D0-C0F2D6DFA09B@cs.umd.edu> To: Khoo Yit Phang X-CSD-MailScanner-ID: 371EAB411F8.A719A X-CSD-MailScanner: Found to be clean X-CSD-MailScanner-SpamCheck: not spam, SpamAssassin (not cached, score=-50, required 5, autolearn=not spam, ALL_TRUSTED -50.00) X-CSD-MailScanner-From: khooyp@cs.umd.edu X-CSD-MailScanner-Watermark: 1326925315.4017@EAMqfGIZGDvF5WsbjLtlwA Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2012-01/txt/msg00386.txt.bz2 --Apple-Mail-10-120964043 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Content-length: 555 Hi, On Jan 11, 2012, at 4:45 PM, Khoo Yit Phang wrote: >=20 > On Jan 11, 2012, at 4:06 PM, Tom Tromey wrote: >=20 >>>>>>> "Yit" =3D=3D Khoo Yit Phang writes: >>=20 >> Yit> I've written a patch to GDB's Python support to allow SIGINT to >> Yit> interrupt a running script to address the bug >> Yit> http://sourceware.org/bugzilla/show_bug.cgi?id=3D13265 (I've attach= ed my >> Yit> patch to that page). >>=20 >> See my other note about how to send the patch; >=20 > I'll attach a patch in a moment. Here it is. Yit January 11, 2012 --Apple-Mail-10-120964043 Content-Disposition: attachment; filename=python-handle-sigint.diff Content-Type: application/octet-stream; name="python-handle-sigint.diff" Content-Transfer-Encoding: 7bit Content-length: 3816 Allow SIGINT to interrupt the "python" command. gdb/Changelog: 2012-01-11 Khoo Yit Phang Allow SIGINT to interrupt the "python" command. * python/python-internal.h (gdbpy_suspend_sigint_handler): New prototype> * python/python.c (gdbpy_saved_sigint_handler): New variable. (gdbpy_handle_sigint): New function. (gdbpy_restore_sigint_handler): Ditto. (gdbpy_install_sigint_handler): Ditto. (gdbpy_resume_sigint_handler): Ditto. (gdbpy_suspend_sigint_handler): Ditto. (restore_python_env): Call gdbpy_install_sigint_handler. (ensure_python_env): Call gdbpy_restore_sigint_handler. (python_command): Move call to ensure_python_env closer to Python entry. (execute_gdb_command): Call gdbpy_suspend_sigint_handler. diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -102,8 +102,11 @@ #include "command.h" #include "breakpoint.h" +#include "defs.h" #include "exceptions.h" +struct cleanup *gdbpy_suspend_sigint_handler (void); + enum gdbpy_iter_kind { iter_keys, iter_values, iter_items }; struct block; diff --git a/gdb/python/python.c b/gdb/python/python.c --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -97,6 +97,51 @@ struct gdbarch *python_gdbarch; const struct language_defn *python_language; +/* When entering Python, install a Python-specific SIGINT handler; + restore the original handler upon exit. */ + +static sig_t gdbpy_saved_sigint_handler; + +static void +gdbpy_handle_sigint (int sig) +{ + PyErr_SetInterrupt (); +} + +static void +gdbpy_restore_sigint_handler (void) +{ + signal (SIGINT, gdbpy_saved_sigint_handler); +} + +static void +gdbpy_install_sigint_handler (void) +{ + gdbpy_saved_sigint_handler = signal (SIGINT, gdbpy_handle_sigint); +} + +/* When calling GDB functions from Python, temporarily suspend the + Python-specific SIGINT handler and restore the original handler + (currently only called by gdb.execute since it's most likely to + take a long time). + + When resuming (upon cleanup), note that the saved SIGINT handler + is updated to the handler installed at that time, to propagate + change to the SIGINT handler down the call stack. */ + +static void +gdbpy_resume_sigint_handler (void *s) +{ + gdbpy_install_sigint_handler (); +} + +struct cleanup * +gdbpy_suspend_sigint_handler (void) +{ + gdbpy_restore_sigint_handler (); + return make_cleanup (gdbpy_resume_sigint_handler, NULL); +} + /* Restore global language and architecture and Python GIL state when leaving the Python interpreter. */ @@ -123,6 +168,8 @@ PyErr_Restore (env->error_type, env->error_value, env->error_traceback); + gdbpy_restore_sigint_handler (); + PyGILState_Release (env->state); python_gdbarch = env->gdbarch; python_language = env->language; @@ -142,6 +189,8 @@ env->gdbarch = python_gdbarch; env->language = python_language; + gdbpy_install_sigint_handler (); + python_gdbarch = gdbarch; python_language = language; @@ -261,15 +310,15 @@ { struct cleanup *cleanup; - cleanup = ensure_python_env (get_current_arch (), current_language); - - make_cleanup_restore_integer (&interpreter_async); + cleanup = make_cleanup_restore_integer (&interpreter_async); interpreter_async = 0; while (arg && *arg && isspace (*arg)) ++arg; if (arg && *arg) { + ensure_python_env (get_current_arch (), current_language); + if (PyRun_SimpleString (arg)) error (_("Error while executing Python code.")); } @@ -442,6 +491,7 @@ char *copy = xstrdup (arg); struct cleanup *cleanup = make_cleanup (xfree, copy); + gdbpy_suspend_sigint_handler (); make_cleanup_restore_integer (&interpreter_async); interpreter_async = 0; --Apple-Mail-10-120964043--