From patchwork Fri Dec 1 15:40:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 81124 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4998A3861849 for ; Fri, 1 Dec 2023 15:41:33 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd2a.google.com (mail-io1-xd2a.google.com [IPv6:2607:f8b0:4864:20::d2a]) by sourceware.org (Postfix) with ESMTPS id 83064385C6EF for ; Fri, 1 Dec 2023 15:41:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 83064385C6EF Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 83064385C6EF Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::d2a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701445264; cv=none; b=N0CYJHc1BnH6FkjgAebuZzEzgFWswba438whu7dVaz2XJcGZyjw+3VcTMwqwgoJ4jktqmLD2dXItsQXqQnZz/urDFqW8ZYedZtGlNhQSixVj7ItKU9Xc5+CL0MDviQ34p1LaYmDNEB14IN3HNkMXXwuuH23b3t44JINiRp0/8PM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701445264; c=relaxed/simple; bh=SzE556DiNRBDH+fzM6ofXfS62w69qpxrhdMjjKUhGwk=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=m+RSYZx9aT+93sXa53Vy9BYdnvSwNyBY1j1fKogBhSZlzI9m+1ukMCb/xO1zrqvPs8XQPUnhflqAW0ucFxm0h2gy377c1ormw6T7bA1x761IHA1Wc9qlPkm/yIUBJuCLPbb3XmUsnS3nDy3Z57AEViZlag/7V/DxKFCg+8uwvNM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-io1-xd2a.google.com with SMTP id ca18e2360f4ac-7b357e43cc2so66302039f.1 for ; Fri, 01 Dec 2023 07:41:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1701445261; x=1702050061; darn=sourceware.org; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=HMG/GrWzqIzWBlRxLLTuKZJYsYWrEdidI0Z23XILRN8=; b=BJrGxY2jRlch095OX5ID8DSnqCsr92G2vZELMGp9Xc4NJ+m5w9/HwPpbmT26LsTC96 mrDmfFdrr9zOoBcPeDarsGiqImuOUYvJOngqCD0wgdZtekpI7hQUkUgSCwqzaelHTMO1 baLox3IKkFLyrmSnKfFuii036vxhY6BRLlpD/DQ67VtfUaqOa4iZGu3/cRxOdIiJvoOO ntAn3ekPYxeECF4ykG//TqW4AB3p/M1A1kmrnTmGBzUkSU6SeGLWfH5kB0NQTJ6VKIV7 4azvEhTGC4dn/r6wdGDnCiShaXQ28BzmerrNolNM8fYvTBnDlBqLnzyKM24165AX3qni wATg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701445261; x=1702050061; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HMG/GrWzqIzWBlRxLLTuKZJYsYWrEdidI0Z23XILRN8=; b=a3aeDYOwAkfdcL/XZiKzZV73p5f1wIEzSpXkxThXkZxyzu1R/oJzb1jHa1wPZ3EZ5s YWjfcSWOuug7i6EQMci5cA0sGgzOP72yiiodFdERTvM/Ki8AgEzKyxVP2RwxXOQHrJog Fz5c4wDc4c9dFgBG0kyLJC7Z2hqRkcN0W7OZDebxWJRMNooq4QsVtaaedFe67SLdNqHL Yimjw+yStgWLRcFPl87INMj4XmWXmiDLbBbAwtg1lQREAMozNhn2dGDIk1/kkzm4qlrF KO4SL/ZBLQaEl5jr+L7w8vkALV3zhXl+g37pXNF9vPGXRZPDqqofrGXYJzdmrcUzqUV+ pyFg== X-Gm-Message-State: AOJu0YwdA2kQYSvl+FSCSfxcS6v00XFc4xMePthoMfRnFPjc/IIKSAhS 9hAF2+Qz1vCv5Ajp+/dEI6i/fK9geyD5CD8n+gs= X-Google-Smtp-Source: AGHT+IGYG6EsxEr4MMRoMk2lvzvEHngNnQQWe19+Aibvl8KWVovxUnVdycHqmLKkAjyqlYZJkgRreQ== X-Received: by 2002:a6b:6011:0:b0:7b0:1aa3:1bb9 with SMTP id r17-20020a6b6011000000b007b01aa31bb9mr21887045iog.20.1701445261597; Fri, 01 Dec 2023 07:41:01 -0800 (PST) Received: from localhost.localdomain (97-122-77-73.hlrn.qwest.net. [97.122.77.73]) by smtp.gmail.com with ESMTPSA id f4-20020a02cac4000000b0046662226e54sm906829jap.168.2023.12.01.07.41.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 07:41:01 -0800 (PST) From: Tom Tromey Date: Fri, 01 Dec 2023 08:40:59 -0700 Subject: [PATCH 3/6] Introduce gdb.interrupt MIME-Version: 1.0 Message-Id: <20231201-dap-cancel-v1-3-872022fc328a@adacore.com> References: <20231201-dap-cancel-v1-0-872022fc328a@adacore.com> In-Reply-To: <20231201-dap-cancel-v1-0-872022fc328a@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.4 X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org 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 --- 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; +} + /* 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\