public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Khoo Yit Phang <khooyp@cs.umd.edu>
To: Khoo Yit Phang <khooyp@cs.umd.edu>
Cc: Tom Tromey <tromey@redhat.com>, gdb-patches@sourceware.org
Subject: Re: Handle SIGINT in Python
Date: Wed, 11 Jan 2012 22:46:00 -0000	[thread overview]
Message-ID: <74929AB6-8260-4E42-B9E4-D533511D718F@cs.umd.edu> (raw)
In-Reply-To: <1FE2845A-162C-48D7-A3D0-C0F2D6DFA09B@cs.umd.edu>

[-- Attachment #1: Type: text/plain, Size: 559 bytes --]

Hi,

On Jan 11, 2012, at 4:45 PM, Khoo Yit Phang wrote:
> 
> On Jan 11, 2012, at 4:06 PM, Tom Tromey wrote:
> 
>>>>>>> "Yit" == Khoo Yit Phang <khooyp@cs.umd.edu> writes:
>> 
>> 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=13265 (I've attached my
>> Yit> patch to that page).
>> 
>> See my other note about how to send the patch;
> 
> I'll attach a patch in a moment.

Here it is.

Yit
January 11, 2012


[-- Attachment #2: python-handle-sigint.diff --]
[-- Type: application/octet-stream, Size: 3816 bytes --]

Allow SIGINT to interrupt the "python" command.

gdb/Changelog:

2012-01-11  Khoo Yit Phang <khooyp@cs.umd.edu>

	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;
 

  reply	other threads:[~2012-01-11 22:22 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-10 21:31 Khoo Yit Phang
2012-01-10 21:47 ` Doug Evans
2012-01-10 22:09   ` Khoo Yit Phang
2012-01-11 20:59   ` Tom Tromey
2012-01-11 21:06     ` Paul_Koning
2012-01-11 21:23       ` Tom Tromey
2012-01-12  0:54     ` Doug Evans
2012-01-12 15:52       ` Kevin Pouget
2012-01-12 16:48         ` Paul_Koning
2012-01-13 10:55           ` Kevin Pouget
2012-01-13 12:11             ` Paul_Koning
2012-01-10 21:49 ` Tom Tromey
2012-01-11 21:15 ` Tom Tromey
2012-01-11 21:49   ` Khoo Yit Phang
2012-01-11 22:46     ` Khoo Yit Phang [this message]
2012-01-20 21:40     ` Tom Tromey
2012-01-22 16:36       ` Khoo Yit Phang
2012-01-22 20:54         ` Khoo Yit Phang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=74929AB6-8260-4E42-B9E4-D533511D718F@cs.umd.edu \
    --to=khooyp@cs.umd.edu \
    --cc=gdb-patches@sourceware.org \
    --cc=tromey@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).