[3/6] Introduce gdb.interrupt

Message ID 20231201-dap-cancel-v1-3-872022fc328a@adacore.com
State New
Headers
Series Implement DAP cancellation |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed

Commit Message

Tom Tromey Dec. 1, 2023, 3:40 p.m. UTC
  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.
---
 gdb/NEWS            |  2 ++
 gdb/doc/python.texi | 11 +++++++++++
 gdb/python/python.c | 19 +++++++++++++++++++
 3 files changed, 32 insertions(+)
  

Comments

Eli Zaretskii Dec. 1, 2023, 3:56 p.m. UTC | #1
> From: Tom Tromey <tromey@adacore.com>
> Date: Fri, 01 Dec 2023 08:40:59 -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.

Thanks.

> diff --git a/gdb/NEWS b/gdb/NEWS
> index 1073e38dfc6..0c44f0253f6 100644
> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -57,6 +57,8 @@ show remote thread-options-packet
>       sub-classed to create handlers for objfiles with missing debug
>       information.
>  
> +  ** New function gdb.interrupt(), that interrupts GDB's main thread.
> +
>  * New remote packets

Do we have to mention the main thread?  Why not say just that it
interrupts GDB as Ctrl-C keypress would?

> +@defun gdb.interrupt ()
> +This causes the main thread of @value{GDBN} to react as if the user
> +had typed a control-C character at the terminal.

Same here: I would avoid talking about the GDB's main thread, as I
think it just muddies the waters, and could really confuse someone who
doesn't have a good mental picture of what threads in general and the
main thread in particular do in GDB.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
  
Tom Tromey Dec. 11, 2023, 3:49 p.m. UTC | #2
>>>>> "Eli" == Eli Zaretskii <eliz@gnu.org> writes:

>> +  ** New function gdb.interrupt(), that interrupts GDB's main thread.
>> +
>> * New remote packets

Eli> Do we have to mention the main thread?  Why not say just that it
Eli> interrupts GDB as Ctrl-C keypress would?

>> +@defun gdb.interrupt ()
>> +This causes the main thread of @value{GDBN} to react as if the user
>> +had typed a control-C character at the terminal.

Eli> Same here: I would avoid talking about the GDB's main thread, as I
Eli> think it just muddies the waters, and could really confuse someone who
Eli> doesn't have a good mental picture of what threads in general and the
Eli> main thread in particular do in GDB.

I'll rewrite these in v2.

Tom
  

Patch

diff --git a/gdb/NEWS b/gdb/NEWS
index 1073e38dfc6..0c44f0253f6 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -57,6 +57,8 @@  show remote thread-options-packet
      sub-classed to create handlers for objfiles with missing debug
      information.
 
+  ** New function gdb.interrupt(), that interrupts GDB's main thread.
+
 * New remote packets
 
 New stop reason: clone
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index b65991bbad0..7d6b7e82edb 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -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 the main thread of @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
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 8a36673a3e4..832f374b39f 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -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;
+}
+
 
 
 /* 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\