]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Introduce gdb.interrupt
authorTom Tromey <tromey@adacore.com>
Thu, 16 Nov 2023 17:44:42 +0000 (10:44 -0700)
committerTom Tromey <tromey@adacore.com>
Mon, 11 Dec 2023 18:44:33 +0000 (11:44 -0700)
DAP cancellation needs a way to interrupt whatever is happening on
gdb's main thread -- whether that is the inferior, a gdb CLI command,
or Python code.

This patch adds a new gdb.interrupt() function for this purpose.  It
simply sets the quit flag and lets gdb do the rest.

No tests in this patch -- instead this is tested via the DAP
cancellation tests.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Reviewed-By: Kévin Le Gouguec <legouguec@adacore.com>
gdb/NEWS
gdb/doc/python.texi
gdb/python/python.c

index 7ffbcbd59e892e5d882395feca889d34d263c354..faa312a7c15dfcc179c02a35faa3af28280c13d1 100644 (file)
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -79,6 +79,9 @@ show remote thread-options-packet
      dictionary that carries the same information as an MI "*stopped"
      event.
 
+  ** New function gdb.interrupt(), that interrupts GDB as if the user
+     typed control-c.
+
 * Debugger Adapter Protocol changes
 
   ** GDB now emits the "process" event.
index 39137aab739f2152a9f9c02bc0c3cc440585c259..d74defeec0c36cc0a8e9a618797f2e3ef91d033c 100644 (file)
@@ -664,6 +664,17 @@ this an easy-to-use drop-in replacement for creating threads that will
 work well in @value{GDBN}.
 @end deftp
 
+@defun gdb.interrupt ()
+This causes @value{GDBN} to react as if the user had typed a control-C
+character at the terminal.  That is, if the inferior is running, it is
+interrupted; if a @value{GDBN} command is executing, it is stopped;
+and if a Python command is running, @code{KeyboardInterrupt} will be
+raised.
+
+Unlike most Python APIs in @value{GDBN}, @code{interrupt} is
+thread-safe.
+@end defun
+
 @defun gdb.post_event (event)
 Put @var{event}, a callable object taking no arguments, into
 @value{GDBN}'s internal event queue.  This callable will be invoked at
index 8a36673a3e4c0bef8533d259d8c13118e650df1a..832f374b39fee67695e1dd0a65ac5423b6a11059 100644 (file)
@@ -1118,6 +1118,23 @@ gdbpy_post_event (PyObject *self, PyObject *args)
   Py_RETURN_NONE;
 }
 
+/* Interrupt the current operation on the main thread.  */
+static PyObject *
+gdbpy_interrupt (PyObject *self, PyObject *args)
+{
+  {
+    /* Make sure the interrupt isn't delivered immediately somehow.
+       This probably is not truly needed, but at the same time it
+       seems more clear to be explicit about the intent.  */
+    gdbpy_allow_threads temporarily_exit_python;
+    scoped_disable_cooperative_sigint_handling no_python_sigint;
+
+    set_quit_flag ();
+  }
+
+  Py_RETURN_NONE;
+}
+
 \f
 
 /* This is the extension_language_ops.before_prompt "method".  */
@@ -2678,6 +2695,8 @@ Parse String as an expression, evaluate it, and return the result as a Value."
 
   { "post_event", gdbpy_post_event, METH_VARARGS,
     "Post an event into gdb's event loop." },
+  { "interrupt", gdbpy_interrupt, METH_NOARGS,
+    "Interrupt gdb's current operation." },
 
   { "target_charset", gdbpy_target_charset, METH_NOARGS,
     "target_charset () -> string.\n\