From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id 5314B385829A; Mon, 11 Dec 2023 18:54:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5314B385829A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1702320859; bh=uktvOIF/ocpIx4AmE337leMwRq4KBumpp/8yhtNg4kM=; h=From:To:Subject:Date:From; b=GRu/MTq3EzAb0liVdglCvFPt02M7WYOyPQbL0Xb8NA1pMs6cwLQcNSpPISCLSTXaF cOtKmfu4GPjAflgilUI0IsGOAFfkPTQo3yWTYdzk4LEUysgQY9KVy8JBivbyfPUBdE LCUiRFrNEBltJ2ziUiTREtpdLMWdkR8ImtXmLFWc= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom Tromey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Introduce gdb.interrupt X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: f895e1592d5f4e58776e6a2d7ccdcc028cc5df80 X-Git-Newrev: 3a12e74ecedfc416aafeda26dbe335ad7962a283 Message-Id: <20231211185419.5314B385829A@sourceware.org> Date: Mon, 11 Dec 2023 18:54:19 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D3a12e74ecedf= c416aafeda26dbe335ad7962a283 commit 3a12e74ecedfc416aafeda26dbe335ad7962a283 Author: Tom Tromey Date: Thu Nov 16 10:44:42 2023 -0700 Introduce gdb.interrupt =20 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. =20 This patch adds a new gdb.interrupt() function for this purpose. It simply sets the quit flag and lets gdb do the rest. =20 No tests in this patch -- instead this is tested via the DAP cancellation tests. =20 Reviewed-By: Eli Zaretskii Reviewed-By: K=C3=A9vin Le Gouguec Diff: --- gdb/NEWS | 3 +++ gdb/doc/python.texi | 11 +++++++++++ gdb/python/python.c | 19 +++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/gdb/NEWS b/gdb/NEWS index 7ffbcbd59e8..faa312a7c15 100644 --- 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. =20 + ** New function gdb.interrupt(), that interrupts GDB as if the user + typed control-c. + * Debugger Adapter Protocol changes =20 ** GDB now emits the "process" event. diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index 39137aab739..d74defeec0c 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 t= hreads that will work well in @value{GDBN}. @end deftp =20 +@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 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; } =20 +/* 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; +} + =0C =20 /* This is the extension_language_ops.before_prompt "method". */ @@ -2678,6 +2695,8 @@ Parse String as an expression, evaluate it, and retur= n the result as a Value." =20 { "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." }, =20 { "target_charset", gdbpy_target_charset, METH_NOARGS, "target_charset () -> string.\n\