public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 3/6] Introduce gdb.interrupt
Date: Fri, 01 Dec 2023 08:40:59 -0700	[thread overview]
Message-ID: <20231201-dap-cancel-v1-3-872022fc328a@adacore.com> (raw)
In-Reply-To: <20231201-dap-cancel-v1-0-872022fc328a@adacore.com>

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(+)

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;
+}
+
 \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\

-- 
2.41.0


  parent reply	other threads:[~2023-12-01 15:41 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-01 15:40 [PATCH 0/6] Implement DAP cancellation Tom Tromey
2023-12-01 15:40 ` [PATCH 1/6] Clean up handling of DAP not-stopped response Tom Tromey
2023-12-01 15:40 ` [PATCH 2/6] Move DAP JSON reader to its own thread Tom Tromey
2023-12-01 15:40 ` Tom Tromey [this message]
2023-12-01 15:56   ` [PATCH 3/6] Introduce gdb.interrupt Eli Zaretskii
2023-12-11 15:49     ` Tom Tromey
2023-12-01 15:41 ` [PATCH 4/6] Rename a couple of DAP procs in the testsuite Tom Tromey
2023-12-01 15:41 ` [PATCH 5/6] Catch BaseException in send_gdb_with_response Tom Tromey
2023-12-01 16:29   ` Kévin Le Gouguec
2023-12-01 17:51     ` Tom Tromey
2023-12-01 15:41 ` [PATCH 6/6] Implement DAP cancellation Tom Tromey
2023-12-01 15:57   ` Eli Zaretskii
2023-12-11 15:05     ` Tom Tromey
2023-12-11 15:44       ` Eli Zaretskii
2023-12-05  9:17 ` [PATCH 0/6] " Kévin Le Gouguec

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=20231201-dap-cancel-v1-3-872022fc328a@adacore.com \
    --to=tromey@adacore.com \
    --cc=gdb-patches@sourceware.org \
    /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).