From patchwork Mon Dec 11 16:02:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 81913 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 BDFAF3856DD4 for ; Mon, 11 Dec 2023 16:02:36 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-ot1-x329.google.com (mail-ot1-x329.google.com [IPv6:2607:f8b0:4864:20::329]) by sourceware.org (Postfix) with ESMTPS id 851513858C56 for ; Mon, 11 Dec 2023 16:02:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 851513858C56 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 851513858C56 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::329 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702310542; cv=none; b=BEh4VCwKkrO6Ea4lKE2WT9Mvfldu/LRdGuNyHDf6HvTRac+czyjwjj8B05leruGELVjYAGRcMoWMCMCaQgeQO+97Ea+XURXRfWFkxhm5htxPG5qiTJmektzHdZrkLxtjL9DvKiasdNLT8/X1WHipFf0xlLdqpXRSJ+6Et+WDIqI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702310542; c=relaxed/simple; bh=v2hF1IY+EYuY59ExjvfYI48lMe8A06ULDRFiHVHgkdY=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=BWhdQyjHNyI5dKcYXFcsyn0NsMboARqTXAKJHLLsc1C8tKNb4iiEl3Wwas1u+UQr8vGmdBYrCgLnruKyJ9yXHtImLAaj1jzwFGq7refvvBTyn+A4vKUSmI4FHDJvySYpY+nO2ltMF/AspvarBL2gr6hskPIqTHMJY+NoPSIq2Wg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ot1-x329.google.com with SMTP id 46e09a7af769-6d9d4193d94so3533385a34.3 for ; Mon, 11 Dec 2023 08:02:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1702310541; x=1702915341; darn=sourceware.org; h=cc: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=SQwLD/rQ+7RuRl2bOn5bHiJrJjo4klR33Bso258oHI8=; b=KT0CxlH5kKUGvjlm89iQC76/OjeTc7TJ8bKLQJLAelCX0SdmnshAXSMzGtspfH+lvO hNwA4Pj7SQcruu7SVBHvRze3+8vHRaO+v9LMmTa3/Sveb8PEukz9dNc8ns2WWGPQ2KPZ Lv0GLMcUBhCuVAE2yMzkzekO8aKSidCugWsgq+bQAM20EvSClgOK3FJ4+W+pP/pvlHdN OcZ+L/Ym4pHMEQaVxh160awtRyi9p54RF1yZ3xprxiCN7LKNUQ2ASSK4Oa6VHwMW1ByK tt6rV6mvFc6m2qB8BZoBejldgaXoOrMML7oXh+Zbpdf4ABvS2jCFFlDnAPRqhNjdE8wm 3Xhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702310541; x=1702915341; h=cc: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=SQwLD/rQ+7RuRl2bOn5bHiJrJjo4klR33Bso258oHI8=; b=tW0RF9VFXK7h+QO0L1K0OQeZJO/q9LYkyu81ByIOWatkF2z/LWQX6PpPFhsFdf88OT tsIwyiC4iYTVSVFi7rqIBwtO/76W2+erOYBxD6tkw44t+rfY8BT3Id8E0IGylKGJ/bAN FKE+gnPoWM/oVzoTaS8yhO24zUYyu6JkaM17mV5yFyNhw1BiRxtF1HqUus+GCwMrVPIg cXH5GDxt4XUMKX92cFSxsd967vlUqWDBAqUS13dI6DrjmnJn/ah5wDQQIYx7UQVc45sR EzDnlWHqPxcJTHoEBmUYtIbDh+8ak2RdYEaFB3gRgAPEtOOPFPprhXKzQwV0U0RYfoMs meTA== X-Gm-Message-State: AOJu0YwVANHME/noUxH6AtWeM+GHETFzh6zK+7Gp8CH7ZB1rrTymPveX Vqf4Vp5Kv1/uwK57xmKEe2xLjw== X-Google-Smtp-Source: AGHT+IGGC3Yo0/97uyCvIc2pb56uXX4znbmq+bE+hZy2Zf2FVF2SoLEofbq4u7YVyQyISnJuROh7tQ== X-Received: by 2002:a05:6870:71ca:b0:1fb:75c:3fe0 with SMTP id p10-20020a05687071ca00b001fb075c3fe0mr6138958oag.64.1702310540799; Mon, 11 Dec 2023 08:02:20 -0800 (PST) Received: from localhost.localdomain (71-211-161-25.hlrn.qwest.net. [71.211.161.25]) by smtp.gmail.com with ESMTPSA id h25-20020a02cd39000000b0042acf934cbasm1879113jaq.72.2023.12.11.08.02.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 08:02:20 -0800 (PST) From: Tom Tromey Date: Mon, 11 Dec 2023 09:02:18 -0700 Subject: [PATCH v2 1/6] Clean up handling of DAP not-stopped response MIME-Version: 1.0 Message-Id: <20231211-dap-cancel-v2-1-db7b52cf0329@adacore.com> References: <20231211-dap-cancel-v2-0-db7b52cf0329@adacore.com> In-Reply-To: <20231211-dap-cancel-v2-0-db7b52cf0329@adacore.com> To: gdb-patches@sourceware.org Cc: =?utf-8?q?K=C3=A9vin_Le_Gouguec?= X-Mailer: b4 0.12.4 X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_ABUSEAT, RCVD_IN_DNSWL_NONE, RCVD_IN_XBL, 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 This patch introduces a new NotStoppedException type and changes the DAP implementation of "not stopped" to use it. I was already touching some code in this area and I thought this looked a little cleaner. This also has the advantage that we can now choose not to log the exception -- previously I was sometimes a bit alarmed when seeing this in the logs, even though it is harmless. Reviewed-By: Kévin Le Gouguec --- gdb/python/lib/gdb/dap/server.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/gdb/python/lib/gdb/dap/server.py b/gdb/python/lib/gdb/dap/server.py index ead911d11da..d865dee31a8 100644 --- a/gdb/python/lib/gdb/dap/server.py +++ b/gdb/python/lib/gdb/dap/server.py @@ -42,6 +42,12 @@ _commands = {} _server = None +# A subclass of Exception that is used solely for reporting that a +# request needs the inferior to be stopped, but it is not stopped. +class NotStoppedException(Exception): + pass + + class Server: """The DAP server class.""" @@ -78,6 +84,9 @@ class Server: if body is not None: result["body"] = body result["success"] = True + except NotStoppedException: + result["success"] = False + result["message"] = "notStopped" except BaseException as e: log_stack() result["success"] = False @@ -169,7 +178,7 @@ def _check_not_running(func): from .events import inferior_running if inferior_running: - raise Exception("notStopped") + raise NotStoppedException() return func(*args, **kwargs) return check From patchwork Mon Dec 11 16:02:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 81914 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 CA362385C413 for ; Mon, 11 Dec 2023 16:02:41 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x131.google.com (mail-il1-x131.google.com [IPv6:2607:f8b0:4864:20::131]) by sourceware.org (Postfix) with ESMTPS id 5B5A83858C5F for ; Mon, 11 Dec 2023 16:02:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5B5A83858C5F 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 5B5A83858C5F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702310543; cv=none; b=kG+DHeUm0VSA199VJWa0tiZ1iLNfxdpbkHN2ugh99AceMNj2fSazyt4AsDUDIRUPCHkvHVxOsUxxnJkPifx3BAxK/zey08xKnf1hUjDYIIV7+Y8k7cqcseS0SBETJaQPB2WaPvGKX4GKez8q3+j56Yww9+s+AoKWkPV1Giwqs+I= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702310543; c=relaxed/simple; bh=xFLP2jGXBarBBL6UzOuh2b1S9DzVNKWFQCuB7zCiaNA=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=YctvKF6xLmR4tr9cSRiePEzDZUlliVxupmW3J75Ki/l2hziQRJvk/71YOeRhGmcPvfuJhiEZepC7EGlnyreCqMrUM8u3PRGPHIx6XC+xEObfkQIdhCWX3SCCqpocwNLiClJt7RNECKbudgu4BkrwEcjrGDgj4JZohvnn+c55WCI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-il1-x131.google.com with SMTP id e9e14a558f8ab-35d67aa6951so14306775ab.0 for ; Mon, 11 Dec 2023 08:02:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1702310541; x=1702915341; darn=sourceware.org; h=cc: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=+axXYG10NsDM5RLP71UUKsOb3qlEBpMS2aTh7PNsGcY=; b=eiHIGFSGSs/VD3fZLP2xkB2lB4FXyRj16Q5G412ZrEC5kBASxsGlpPCNj960yDrWnB P6OYZc2sQs8hj5VK1vNwyV993BJZnZrOB62o/QIPPQp8KaT/V1dDtrbQ8EFlcOnb/n7o g+dvfcG7UgVeD2tzn5nIy5NPeXaNaHwHMy/trjnSfmcbtH9v3QoFD4ltOrySEiNQ3jyJ 0txe3WT+flTyfmOtUIibc6kxpTIjv3frn81gvOaJmSWRqup1zAXJmN6HHKhMOElwn/Mm t2cMP/BULM4XW92OczvzCIU+RwTE0kF5ZY9tqnUJ+ZjM8sw8BWX5QKLYEtZjK33+d27j moqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702310541; x=1702915341; h=cc: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=+axXYG10NsDM5RLP71UUKsOb3qlEBpMS2aTh7PNsGcY=; b=JDAuenCH5E8gT8Fg++cecOL3hzPavOo5WzavATTMex5MLXOp7twxk71LT/JhVoAnCI C/Axt/EvfQgrgEuv5r+J0NnPfxvIbXb+4Daxp0THGHfBOLVrXiHpfI3FDNhs9BSFseyU g6wkf6WhRhxK+tvzoeggskK8oU4e92LIriJrOjC7q2flLfhfuDmpKOpUHv0YaJnJ1Flx V2xJvODmHCvXUeoj0wV0j0eYRW7c1OB+WCc2WY9JzcFcFCnGczkSY/oGtHBJj6QY45B1 +8q9BelNESLzuHpNKfpEgpQufjCuNK9XeoXKwEf/d/3yJErHR6CbufmQwP40qSoOGSoZ 8MGQ== X-Gm-Message-State: AOJu0YxNfJbnvoY/c2LGMgsGr5eg3vwWMucbYr/i0I3dG/aCs/EQEOTz zaw1TIqzEdgfXzgdIwskuLc7jA== X-Google-Smtp-Source: AGHT+IEzV1/e5E578oTq56k8fCyrLlcPrDf2TX60xjZ43LlP78mDrh84VCqVDGp2HDSSNZ3HnZNnVg== X-Received: by 2002:a05:6e02:1a2a:b0:35d:726c:5d16 with SMTP id g10-20020a056e021a2a00b0035d726c5d16mr4363579ile.31.1702310541471; Mon, 11 Dec 2023 08:02:21 -0800 (PST) Received: from localhost.localdomain (71-211-161-25.hlrn.qwest.net. [71.211.161.25]) by smtp.gmail.com with ESMTPSA id h25-20020a02cd39000000b0042acf934cbasm1879113jaq.72.2023.12.11.08.02.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 08:02:21 -0800 (PST) From: Tom Tromey Date: Mon, 11 Dec 2023 09:02:19 -0700 Subject: [PATCH v2 2/6] Move DAP JSON reader to its own thread MIME-Version: 1.0 Message-Id: <20231211-dap-cancel-v2-2-db7b52cf0329@adacore.com> References: <20231211-dap-cancel-v2-0-db7b52cf0329@adacore.com> In-Reply-To: <20231211-dap-cancel-v2-0-db7b52cf0329@adacore.com> To: gdb-patches@sourceware.org Cc: =?utf-8?q?K=C3=A9vin_Le_Gouguec?= X-Mailer: b4 0.12.4 X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_ABUSEAT, RCVD_IN_DNSWL_NONE, RCVD_IN_XBL, 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 This changes the DAP server to move the JSON reader to a new thread. This is key to implementing request cancellation, as now requests can be read while an earlier one is being serviced. Reviewed-By: Kévin Le Gouguec --- gdb/python/lib/gdb/dap/server.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/gdb/python/lib/gdb/dap/server.py b/gdb/python/lib/gdb/dap/server.py index d865dee31a8..53a0ca7f448 100644 --- a/gdb/python/lib/gdb/dap/server.py +++ b/gdb/python/lib/gdb/dap/server.py @@ -60,6 +60,9 @@ class Server: # DAP client. Writing is done in a separate thread to avoid # blocking the read loop. self.write_queue = DAPQueue() + # Reading is also done in a separate thread, and a queue of + # requests is kept. + self.read_queue = DAPQueue() self.done = False global _server _server = self @@ -111,6 +114,14 @@ class Server: log("WROTE: <<<" + json.dumps(obj) + ">>>") self.write_queue.put(obj) + # This is run in a separate thread and simply reads requests from + # the client and puts them into a queue. + def _reader_thread(self): + while True: + cmd = read_json(self.in_stream) + log("READ: <<<" + json.dumps(cmd) + ">>>") + self.read_queue.put(cmd) + @in_dap_thread def main_loop(self): """The main loop of the DAP server.""" @@ -118,9 +129,9 @@ class Server: # client, and the thread that reads output from the inferior. start_thread("output reader", self._read_inferior_output) start_json_writer(self.out_stream, self.write_queue) + start_thread("JSON reader", self._reader_thread) while not self.done: - cmd = read_json(self.in_stream) - log("READ: <<<" + json.dumps(cmd) + ">>>") + cmd = self.read_queue.get() result = self._handle_command(cmd) self._send_json(result) events = self.delayed_events From patchwork Mon Dec 11 16:02:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 81916 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 3F083385AC30 for ; Mon, 11 Dec 2023 16:03:07 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x12f.google.com (mail-il1-x12f.google.com [IPv6:2607:f8b0:4864:20::12f]) by sourceware.org (Postfix) with ESMTPS id E91A13858C78 for ; Mon, 11 Dec 2023 16:02:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E91A13858C78 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 E91A13858C78 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::12f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702310544; cv=none; b=eiJpcIFsXwPgmtUnlXSy2575Fe/4/y58O7kv+OI4XyTVTfBVzaLL4HOuKTE1CY+lWX+kZQIk2t4sXIPJiFsevA4Wqp7PW8HPtenWFlO1wspk8jGIz1C4ZZ7QhIsxkMD0ly3hg3GV/i3GA+Vdjfm/QNX2KtpF1s0bnUb6NAJWCgo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702310544; c=relaxed/simple; bh=TuPJiDaxok7J5h9C5JDQSC46gFezfi1L0cxpelx1D84=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=XPXV/HWtUnUBWyIWNdGnybDkgTjhbxUn3jhRVhfd2LtrOCrX1ugXexUpZRKmQ9H9KN4/9lWnkZVLc6UJOTXISkjW7Gw4gdgTfD2o5ztWIeUwX26cCB6UyD+c6MaJceX/+AMFM6M8g8FA40ukPMqCIm1HGhXuHj+yRrBcqgQVTLc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-il1-x12f.google.com with SMTP id e9e14a558f8ab-35d62401a3dso18579915ab.3 for ; Mon, 11 Dec 2023 08:02:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1702310542; x=1702915342; darn=sourceware.org; h=cc: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=iPy/1rADhqoTC7B/T/lEFi84lzFq88HdJ7wmAkR/l1E=; b=WksNmdt2CZkn3gBVGANAyJ8BZmbZGH6zGfafGcEMyFPhnb4dDnIDeO0s0oYUFEZflN Tmd99U4GiaqImLvQOOH7aHB2+QdeOs1bow6EUanaZw2YtSYf8agt6QSlW0iynJdWE0Nz TpGo8XSGB6NRihH5iwRKZYwZrYQJDJRN1hJOtTCxnPny2jNM4jAmK1z+77qKMvz9rbgU OyY4u73LI75cxbQHaTg+2doKnrGQKadtatbvQyk+Fke4y9zK3HSMGNF1f8+WLZTTWta6 c3daVSWpHrSNiMFFW1E0XHaK5pMIY5qIH0vgzBAAx8nadPFWXHJTe9U4TAmgh2Bm4nX+ ypDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702310542; x=1702915342; h=cc: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=iPy/1rADhqoTC7B/T/lEFi84lzFq88HdJ7wmAkR/l1E=; b=AiJL1lERXOuujTvh+WX2tsjdMOxwPLKnFYt0GgYu5o9BxeczKlVCLuiES86cmwTHS2 ZB70V64FVy2ulYgviO3pdsT5k2i81+0wfavr3ZI7O3VjsmOmMoEWUzhsYyk9wLnq3NtN 105zijPgvAzxfBo5sNRx7eyXNHxmgRdEvfPooy8piJd4zjnam5SdJa/sYwBQniNYhBsH p70fxbKr+MwfEaPb2d4yJ8/bamGo46K1ELkbfza68FDHzdmJnEfC9pTXuk+xywAj7FoR CPTjBNE+nekm5HEvWqKgHzlaj2cVfABGJtqmVoU5Nt1eOXpJCz7z+Zdz06k2OUCPS+cG 7zyg== X-Gm-Message-State: AOJu0YxARKC4QzjeAG9XuN0Cia3vsNS00dpf0blk+Hy9F6qzQ9QsU3uL n0i0EFL9aLbbp/lwD0c+ZseqCw== X-Google-Smtp-Source: AGHT+IG7RovIkReMsiXQx02IGtFa4NuLW4xEDFP4Dqbr5Ytci7u4pkPmNHd8lmy0A9O7Tr+FKwnRrg== X-Received: by 2002:a05:6e02:154b:b0:35d:a13f:dac with SMTP id j11-20020a056e02154b00b0035da13f0dacmr5963162ilu.25.1702310542090; Mon, 11 Dec 2023 08:02:22 -0800 (PST) Received: from localhost.localdomain (71-211-161-25.hlrn.qwest.net. [71.211.161.25]) by smtp.gmail.com with ESMTPSA id h25-20020a02cd39000000b0042acf934cbasm1879113jaq.72.2023.12.11.08.02.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 08:02:21 -0800 (PST) From: Tom Tromey Date: Mon, 11 Dec 2023 09:02:20 -0700 Subject: [PATCH v2 3/6] Introduce gdb.interrupt MIME-Version: 1.0 Message-Id: <20231211-dap-cancel-v2-3-db7b52cf0329@adacore.com> References: <20231211-dap-cancel-v2-0-db7b52cf0329@adacore.com> In-Reply-To: <20231211-dap-cancel-v2-0-db7b52cf0329@adacore.com> To: gdb-patches@sourceware.org Cc: Eli Zaretskii , =?utf-8?q?K=C3=A9vin_Le_Gouguec?= X-Mailer: b4 0.12.4 X-Spam-Status: No, score=-9.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_ABUSEAT, RCVD_IN_DNSWL_NONE, RCVD_IN_XBL, 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 Reviewed-By: Kévin Le Gouguec Reviewed-By: Eli Zaretskii --- 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 aaf8faad792..900ac47ada9 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -75,6 +75,9 @@ show remote thread-options-packet sub-classed to create handlers for objfiles with missing debug information. + ** New function gdb.interrupt(), that interrupts GDB as if the user + typed control-c. + * Debugger Adapter Protocol changes ** GDB now emits the "process" event. diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index 3a35c5c2ccc..ede8b1fb6b5 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 @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\ From patchwork Mon Dec 11 16:02:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 81915 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 403A9385C308 for ; Mon, 11 Dec 2023 16:02:52 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x133.google.com (mail-il1-x133.google.com [IPv6:2607:f8b0:4864:20::133]) by sourceware.org (Postfix) with ESMTPS id D751B3858C2A for ; Mon, 11 Dec 2023 16:02:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D751B3858C2A 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 D751B3858C2A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::133 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702310545; cv=none; b=WXOjYuRWl+A6lL4qpLtaXDQCA1vzUpgRB2I4JyMlpTHT4sqtfBWB/iicH0IBZILnCbhy+92wsuT9AaTfKCH8C13KBUhly5AvDvAJPM/Ms8kJl4QWThBMTTFvrDzjaiRALmP2HmspU9uPNPDyjpkuYtJoWoYRB8+/xBHfBYZSHog= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702310545; c=relaxed/simple; bh=hhU3KL+WvfCWEujAkbOEbySRFzqHm4ghidjg9LnygiU=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=Z8TJfp4PbEne3G4dOmVYIWkMZDLuWvQmrqlQBIGpxMLT7ZUDF9SB/QQX5kkqHpF7SsP6DORXX33qX/jn/YGm07cmG59dEv5freE1T9ymycz+ULFjACJYkIowqfk+w0o8lyZwTbr0qO+HeE//IUXzBovZZJPh1LALNmZNVpXgVTM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-il1-x133.google.com with SMTP id e9e14a558f8ab-35d5d54a8eeso11507485ab.1 for ; Mon, 11 Dec 2023 08:02:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1702310543; x=1702915343; darn=sourceware.org; h=cc: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=Erhl31MKHC9yxzJoj/MLLZhbGeH3VycW1sOYw3V6S1o=; b=X4qqok6utgGt4GzYlUguRFJILBCGRoT06hzugcKgaNLcuVrT+K6TDXW7+d3qtkVDYK Dww2VtcQTCNUaprCQH3fi0XJQhYzHk8N+b2DkMdj31noI7CqO5iVcaeZqz5ssH3by5zZ 9gz/A3wUpVRTpBRcud2qqy2hU/kpTHLo2K6A26lpHnn/ErG6XWXcebKWLW3x5fqdj5Xl gRocAhWT/SANJk9NLWAnVXTCp3kkOBsn9lLaHTxiEyIR4xU5zXbqDtEVr+00RYk45i0G gVu991Wg+6/gUS3Wye5AbuZ8uQfTT0mAs4/BrSXuhzrb8x4MQw/Wp8J2qRn4MIXbQZoV wZPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702310543; x=1702915343; h=cc: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=Erhl31MKHC9yxzJoj/MLLZhbGeH3VycW1sOYw3V6S1o=; b=OW9FzeZtZGIPOZXEGaXJ/rc9+/Svy3q28f7aTuq7Jump7onAYdpvIrsHfCsNcONdf7 vmxDH1U4bqkT982daNplx3lSfuKjsG++p98hzIamcIvE0ssH6xGySK3bq3ZMpsYrjJ8Q aIPY6mp+zcFQdfiIBFqPMd0fmji25IDR+WFPhWAaVYaX8Nq41ft2usUT21KLpW+niwvp KxydZPL2idhnZyZwDnbEhq5zWMl/K7C1KDn4PZPhWeQyfbdeEogXjXEfyB5HB7a2PKtY 7oadAiU613gtgzijFRMbQ7OdhRklmrVmAry8Gy58NEXVT7RJN+OAwArDeyymfyp5Birh YJgQ== X-Gm-Message-State: AOJu0YzS8XPVse0R+UEFdoTvXSU9lnGq8NP4n8E18n0XUvJ6psXq44Dm 88yhqGpqwp4/EDopzUl9rEa3i4LD5vmCGkhEcZM= X-Google-Smtp-Source: AGHT+IEjP0+S1Xlcbm1xFw2EfuDMt2ygl9E/XiOyygXMbhY7Oh7AtEfRvt+SNxZDFUdecMSn7Nc5Og== X-Received: by 2002:a05:6e02:1d81:b0:35d:5326:3618 with SMTP id h1-20020a056e021d8100b0035d53263618mr3394736ila.11.1702310543134; Mon, 11 Dec 2023 08:02:23 -0800 (PST) Received: from localhost.localdomain (71-211-161-25.hlrn.qwest.net. [71.211.161.25]) by smtp.gmail.com with ESMTPSA id h25-20020a02cd39000000b0042acf934cbasm1879113jaq.72.2023.12.11.08.02.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 08:02:22 -0800 (PST) From: Tom Tromey Date: Mon, 11 Dec 2023 09:02:21 -0700 Subject: [PATCH v2 4/6] Rename a couple of DAP procs in the testsuite MIME-Version: 1.0 Message-Id: <20231211-dap-cancel-v2-4-db7b52cf0329@adacore.com> References: <20231211-dap-cancel-v2-0-db7b52cf0329@adacore.com> In-Reply-To: <20231211-dap-cancel-v2-0-db7b52cf0329@adacore.com> To: gdb-patches@sourceware.org Cc: =?utf-8?q?K=C3=A9vin_Le_Gouguec?= X-Mailer: b4 0.12.4 X-Spam-Status: No, score=-9.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_ABUSEAT, RCVD_IN_DNSWL_NONE, RCVD_IN_XBL, 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 This renames a couple of DAP procs in the testsuite, to clarify that they are now exported. The cancellation test will need these. Reviewed-By: Kévin Le Gouguec --- gdb/testsuite/lib/dap-support.exp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/gdb/testsuite/lib/dap-support.exp b/gdb/testsuite/lib/dap-support.exp index 82d83d95b9f..b9ac314fee5 100644 --- a/gdb/testsuite/lib/dap-support.exp +++ b/gdb/testsuite/lib/dap-support.exp @@ -111,7 +111,7 @@ proc _dap_send_ton {obj} { # omitted. The sequence number of the request is automatically added, # and this is also the return value. OBJ is assumed to already be in # TON form. -proc _dap_send_request {command {obj {}}} { +proc dap_send_request {command {obj {}}} { # We can construct this directly as a TON object. set result $::dap_seq incr ::dap_seq @@ -186,7 +186,7 @@ proc _dap_read_json {} { # response is seen, this leaves the global "last_ton" set to the TON # for the response. -proc _dap_read_response {cmd num} { +proc dap_read_response {cmd num} { set result {} while 1 { set d [_dap_read_json] @@ -204,11 +204,11 @@ proc _dap_read_response {cmd num} { } } -# A wrapper for _dap_send_request and _dap_read_response. This sends a +# A wrapper for dap_send_request and dap_read_response. This sends a # request to gdb and returns the response as a dict. proc dap_request_and_response {command {obj {}}} { - set seq [_dap_send_request $command $obj] - return [_dap_read_response $command $seq] + set seq [dap_send_request $command $obj] + return [dap_read_response $command $seq] } # Like dap_request_and_response, but also checks that the response From patchwork Mon Dec 11 16:02:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 81918 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 66FDC3857BAE for ; Mon, 11 Dec 2023 16:03:17 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd2e.google.com (mail-io1-xd2e.google.com [IPv6:2607:f8b0:4864:20::d2e]) by sourceware.org (Postfix) with ESMTPS id 7AF34385843F for ; Mon, 11 Dec 2023 16:02:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7AF34385843F 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 7AF34385843F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::d2e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702310545; cv=none; b=KJTpGCKd8i87pjkTRXAUPkM2ZEWssQRXPjroi6OWtXPI/e8y/6oj1d+aLTtN0r3rWh6JOjNChdkpAIJglTueupAbz6IHuMRmdXeww7vjy1FvjpYWP/R7V7DNgx9vX9qs01TMetibbNQWAZBXn7xFhcoxyGss2+GuWVNWIv5NYSE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702310545; c=relaxed/simple; bh=rCfVQUT8mHIXKt3Si7KFF9H/jiAZ7rTu+yVS94QG0NQ=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=HzzbvUz/0iP0UEDhhXV7ULkLze3I6Bs4GTpDTCBbUtgx44wmxdOWSkSG9lGsl3HRLG7YUAFZn+4uPtAF0JoId3mh4+JGJIdWpfxyyP3yTWRVFwQGzo09Xwu4mdUh9aG9g4MRmjSM9Gn3wGgZfCWmBbrfPUm+hUMNzT1qjqfcFFE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-io1-xd2e.google.com with SMTP id ca18e2360f4ac-7b7282c8941so101128039f.3 for ; Mon, 11 Dec 2023 08:02:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1702310543; x=1702915343; 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=95ziRDvc05ggMPrUdaDiKy8qC67noNUgHDEiS1OkPq4=; b=NKBxb2p8jIYTWUdrHAH2iOpbjsugu6pTDZtbAOlqML+JpLwMSK00tFjI/BqcNaMU4J Ee7zoN5WJDyNvEDVr2HkAlOirVGEUbmJUMPhfr5whAhLZAgV8IMjlu4NdDL6ohlT+bBY jooNul80GFEG6hjyXHpJambzD/zK1Xdn19JOoH9hNSxkZCG5b4vkPAwSOJE+y8thouJ1 a5yIrkpV5kg62vM+JynOElvHViQH2hh1eQf8y8bHxCNc+XSQfyaIw9ihDgcmL+eRC2jR oq9bn5X52kTyisLcf9eLMQ4KbFYwtxd0u4wtDMu7kz19FIJGMS1nTThXE49459CPp4tE hOYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702310543; x=1702915343; 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=95ziRDvc05ggMPrUdaDiKy8qC67noNUgHDEiS1OkPq4=; b=TiWn8DaawLCIFrqGLuKtzFFqnp4S5NkEGH8hQDNpie5tWjSsW3DcwnvrVaHL4lvhtp vYuyr68Yn1R6H72MUGX0fSl6CzabzP2uUK7qmxKq/86lVK0pPZE8OvH2PlCHRXvTU5Hs FOhI4ooF8rm0gyundZsn6ffcMhAX18Fppg8J+NUxXzbOMm2zlLQ9lZTOkhAM5I0zGtXu PvK5uExsiL/lA3nxcNeKAjIKOa1Qod0Rr7BoCoAXUE5xE9gj2zYL9hiEFg3u8jHWrHzT JVLBknTp49lIJ9lyj8DY0gUXjUlZfqjU/Wuvs1gt1CAc/VZ+KYXN6ZrE/0Mt4kNOdt1t nHeQ== X-Gm-Message-State: AOJu0YyLX6bBmNHYHY9Nfbdlrxe3ZXIVZ5liBQCtenXIUwwFMQDXkV// cNdOY1SQlnjfYKk8arEf6rg7RH6iywrluJruqxI= X-Google-Smtp-Source: AGHT+IG08tQrZ+oUiw62NVijV+yWAoFqjaBal9tY4iGdqPVJqByKPFxBkZhAtxsJXhXj/io8UdzEZg== X-Received: by 2002:a05:6e02:12ce:b0:35d:59a2:1281 with SMTP id i14-20020a056e0212ce00b0035d59a21281mr7764181ilm.45.1702310543773; Mon, 11 Dec 2023 08:02:23 -0800 (PST) Received: from localhost.localdomain (71-211-161-25.hlrn.qwest.net. [71.211.161.25]) by smtp.gmail.com with ESMTPSA id h25-20020a02cd39000000b0042acf934cbasm1879113jaq.72.2023.12.11.08.02.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 08:02:23 -0800 (PST) From: Tom Tromey Date: Mon, 11 Dec 2023 09:02:22 -0700 Subject: [PATCH v2 5/6] Catch KeyboardInterrupt in send_gdb_with_response MIME-Version: 1.0 Message-Id: <20231211-dap-cancel-v2-5-db7b52cf0329@adacore.com> References: <20231211-dap-cancel-v2-0-db7b52cf0329@adacore.com> In-Reply-To: <20231211-dap-cancel-v2-0-db7b52cf0329@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.4 X-Spam-Status: No, score=-9.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_ABUSEAT, RCVD_IN_DNSWL_NONE, RCVD_IN_XBL, 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 Cancellation will generally be seen by the DAP code as a KeyboardInterrupt. However, this derives from BaseException and not Exception, so a small change is needed to send_gdb_with_response, to forward the exception to the DAP server thread. Reviewed-By: Kévin Le Gouguec --- gdb/python/lib/gdb/dap/startup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gdb/python/lib/gdb/dap/startup.py b/gdb/python/lib/gdb/dap/startup.py index a16b51f7cf5..1d3b94762a6 100644 --- a/gdb/python/lib/gdb/dap/startup.py +++ b/gdb/python/lib/gdb/dap/startup.py @@ -172,11 +172,11 @@ def send_gdb_with_response(fn): try: val = fn() result_q.put(val) - except Exception as e: + except (Exception, KeyboardInterrupt) as e: result_q.put(e) send_gdb(message) val = result_q.get() - if isinstance(val, Exception): + if isinstance(val, (Exception, KeyboardInterrupt)): raise val return val From patchwork Mon Dec 11 16:02:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 81917 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 59E95385701B for ; Mon, 11 Dec 2023 16:03:08 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x131.google.com (mail-il1-x131.google.com [IPv6:2607:f8b0:4864:20::131]) by sourceware.org (Postfix) with ESMTPS id 583BC38582AD for ; Mon, 11 Dec 2023 16:02:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 583BC38582AD 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 583BC38582AD Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702310547; cv=none; b=lcRDRe3zSWqbDoj4EbL3LEL8FSOVBm55WmWQn1MI95PRhSihV7VyaHWbxBUgnI8qTXMtPzm3fb0nMAqwRe1dqW05Tp24poO305wzzOghFRi42X5G32vMGbtupWEJ1y2mJDmIQAHto7PkrIcyInZhHVBN6j+Gafwjazwk/H+6ozk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702310547; c=relaxed/simple; bh=FtTNIDZpSKPv+FfpAd8BmnqUPzOdP8ZEanBqI9ld65U=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=peteqJ8Ntf0Sh2hfoScyepThj3+HyJPF5usH626PJFk4ETMl1vbjjccRCv6UKNLagHuT+BBaN3jMqpE7FZPTsYXyHZ9UZeDjrDDzKSvYPF67v61gv3bwoBqlyBpOakFRG9lDqsGURYjCFq0lkpj+GJaDNMTfAsFAAzFRBE45500= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-il1-x131.google.com with SMTP id e9e14a558f8ab-35d3b220b64so18604445ab.2 for ; Mon, 11 Dec 2023 08:02:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1702310544; x=1702915344; darn=sourceware.org; h=cc: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=ksxN0tKg72qo/xSongPZd5qAw/nyO3t6aPk3JO/T3wY=; b=MDY7k+UFNmlBqiy7f1AHB1bCPiNpMHttFwtpKzGU2LqRu84ESo9exRA2R80UqSMITC yzmbjM9DByx1DH6oNEPsYhp+Zvvq7e+CjrJcy96kixxKwjtonTHexvEFVj1AtrV/RfPc vNg7MQSEkiIwIQggO5nvlWHzC08oUZmfL127DovKHTQmSwcngpoiDFKWBSnx9YYDKvds Oorq0LMX/72DTbueeKwrbckgFcGl1xWpg+IUw6maAwUCTOH87KfjHltT5+DbG3iCb9mj vQpXBGx1ULpNPDqkil76At55ZkwaSoHl4zrSDbLo3DDeV6ZmqKbAa5B/LKCPt8UKzBqG mhQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702310544; x=1702915344; h=cc: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=ksxN0tKg72qo/xSongPZd5qAw/nyO3t6aPk3JO/T3wY=; b=c9dqGthFaoiChzZ0MWHFBVmmgrpoXLnQNAjcuPuVX8mJ/WXAVF4GYBppi8Os5nLNKD o7o/LN84t3J0bn+iRwiUlEhZKQfmh5KaqLby4cqmk90HZmuy3yXPXDCHhN7/L8m05P2V tCqayGrdfau5yjBU+iGAGlTa1ovB/BKQFlbBcM1WKNeykEMyTQyojqrnv1axaeFDGP8m 5Kd69AgLtViWy5zYU4a+v4n+KwKdqZbEmUPuS5J9yBYyVcCIZxsEkDZVd+3XGWTm3hHl LjhcoiTDzwVx/u1GgAdOeV5ozeK1JmWR95VQA7DwEuQMrAv4Uzni+w164qTh627jQRkI NrBQ== X-Gm-Message-State: AOJu0YxREYzUVmexJBce0gkEATx/8uARMkf4AlI/fJxXzwqoh9PZTs60 DtslbCZMBVXfUXpB+U7iebiBpJLsEMIxUPzjVzQ= X-Google-Smtp-Source: AGHT+IE2VdyO2JqcZlBEVZ/HnD31qr6NFTsuQOYGL6PgPKtmMaEgpO30gI4OPTLOdT8p0/ZCluhQGw== X-Received: by 2002:a05:6e02:12e6:b0:35d:4e2f:aacc with SMTP id l6-20020a056e0212e600b0035d4e2faaccmr6239223iln.22.1702310544472; Mon, 11 Dec 2023 08:02:24 -0800 (PST) Received: from localhost.localdomain (71-211-161-25.hlrn.qwest.net. [71.211.161.25]) by smtp.gmail.com with ESMTPSA id h25-20020a02cd39000000b0042acf934cbasm1879113jaq.72.2023.12.11.08.02.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 08:02:24 -0800 (PST) From: Tom Tromey Date: Mon, 11 Dec 2023 09:02:23 -0700 Subject: [PATCH v2 6/6] Implement DAP cancellation MIME-Version: 1.0 Message-Id: <20231211-dap-cancel-v2-6-db7b52cf0329@adacore.com> References: <20231211-dap-cancel-v2-0-db7b52cf0329@adacore.com> In-Reply-To: <20231211-dap-cancel-v2-0-db7b52cf0329@adacore.com> To: gdb-patches@sourceware.org Cc: Eli Zaretskii , =?utf-8?q?K=C3=A9vin_Le_Gouguec?= X-Mailer: b4 0.12.4 X-Spam-Status: No, score=-9.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_ABUSEAT, RCVD_IN_DNSWL_NONE, RCVD_IN_XBL, 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 This implements DAP cancellation. A new object is introduced that handles the details of cancellation. While cancellation is inherently racy, this code attempts to make it so that gdb doesn't inadvertently cancel the wrong request. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30472 Approved-By: Eli Zaretskii Reviewed-By: Kévin Le Gouguec --- gdb/NEWS | 2 + gdb/doc/gdb.texinfo | 16 +++++++ gdb/python/lib/gdb/dap/server.py | 91 ++++++++++++++++++++++++++++++++++++++-- gdb/testsuite/gdb.dap/pause.exp | 71 +++++++++++++++++++++++++++++++ 4 files changed, 177 insertions(+), 3 deletions(-) diff --git a/gdb/NEWS b/gdb/NEWS index 900ac47ada9..3e804fb1e53 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -82,6 +82,8 @@ show remote thread-options-packet ** GDB now emits the "process" event. + ** GDB now supports the "cancel" request. + * New remote packets New stop reason: clone diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 58685a77fd4..6e4adf512ee 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -39595,6 +39595,22 @@ to return the bytes of each instruction in an implementation-defined format. @value{GDBN} implements this by sending a string with the bytes encoded in hex, like @code{"55a2b900"}. +When the @code{repl} context is used for the @code{evaluate} request, +@value{GDBN} evaluates the provided expression as a CLI command. + +Evaluation in general can cause the inferior to continue execution. +For example, evaluating the @code{continue} command could do this, as +could evaluating an expression that involves an inferior function +call. + +@code{repl} evaluation can also cause @value{GDBN} to appear to stop +responding to requests, for example if a CLI script does a lengthy +computation. + +Evaluations like this can be interrupted using the DAP @code{cancel} +request. (In fact, @code{cancel} should work for any request, but it +is unlikely to be useful for most of them.) + @node JIT Interface @chapter JIT Compilation Interface @cindex just-in-time compilation diff --git a/gdb/python/lib/gdb/dap/server.py b/gdb/python/lib/gdb/dap/server.py index 53a0ca7f448..44dffb1b809 100644 --- a/gdb/python/lib/gdb/dap/server.py +++ b/gdb/python/lib/gdb/dap/server.py @@ -14,8 +14,11 @@ # along with this program. If not, see . import functools +import gdb +import heapq import inspect import json +import threading from .io import start_json_writer, read_json from .startup import ( @@ -48,6 +51,52 @@ class NotStoppedException(Exception): pass +# This is used to handle cancellation requests. It tracks all the +# needed state, so that we can cancel both requests that are in flight +# as well as queued requests. +class CancellationHandler: + def __init__(self): + # Methods on this class acquire this lock before proceeding. + self.lock = threading.Lock() + # The request currently being handled, or None. + self.in_flight = None + self.reqs = [] + + def starting(self, req): + """Call at the start of the given request. + + Throws the appropriate exception if the request should be + immediately cancelled.""" + with self.lock: + self.in_flight = req + while len(self.reqs) > 0 and self.reqs[0] <= req: + if heapq.heappop(self.reqs) == req: + raise KeyboardInterrupt() + + def done(self, req): + """Indicate that the request is done.""" + with self.lock: + self.in_flight = None + + def cancel(self, req): + """Call to cancel a request. + + If the request has already finished, this is ignored. + If the request is in flight, it is interrupted. + If the request has not yet been seen, the cancellation is queued.""" + with self.lock: + if req == self.in_flight: + gdb.interrupt() + else: + # We don't actually ignore the request here, but in + # the 'starting' method. This way we don't have to + # track as much state. Also, this implementation has + # the weird property that a request can be cancelled + # before it is even sent. It didn't seem worthwhile + # to try to check for this. + heapq.heappush(self.reqs, req) + + class Server: """The DAP server class.""" @@ -64,6 +113,7 @@ class Server: # requests is kept. self.read_queue = DAPQueue() self.done = False + self.canceller = CancellationHandler() global _server _server = self @@ -71,13 +121,14 @@ class Server: # PARAMS is just a dictionary from the JSON. @in_dap_thread def _handle_command(self, params): - # We don't handle 'cancel' for now. + req = params["seq"] result = { - "request_seq": params["seq"], + "request_seq": req, "type": "response", "command": params["command"], } try: + self.canceller.starting(req) if "arguments" in params: args = params["arguments"] else: @@ -90,10 +141,15 @@ class Server: except NotStoppedException: result["success"] = False result["message"] = "notStopped" + except KeyboardInterrupt: + # This can only happen when a request has been canceled. + result["success"] = False + result["message"] = "cancelled" except BaseException as e: log_stack() result["success"] = False result["message"] = str(e) + self.canceller.done(req) return result # Read inferior output and sends OutputEvents to the client. It @@ -115,11 +171,25 @@ class Server: self.write_queue.put(obj) # This is run in a separate thread and simply reads requests from - # the client and puts them into a queue. + # the client and puts them into a queue. A separate thread is + # used so that 'cancel' requests can be handled -- the DAP thread + # will normally block, waiting for each request to complete. def _reader_thread(self): while True: cmd = read_json(self.in_stream) log("READ: <<<" + json.dumps(cmd) + ">>>") + # Be extra paranoid about the form here. If anything is + # missing, it will be put in the queue and then an error + # issued by ordinary request processing. + if ( + "command" in cmd + and cmd["command"] == "cancel" + and "arguments" in cmd + # gdb does not implement progress, so there's no need + # to check for progressId. + and "requestId" in cmd["arguments"] + ): + self.canceller.cancel(cmd["arguments"]["requestId"]) self.read_queue.put(cmd) @in_dap_thread @@ -316,3 +386,18 @@ def disconnect(*, terminateDebuggee: bool = False, **args): if terminateDebuggee: send_gdb_with_response("kill") _server.shutdown() + + +@request("cancel", on_dap_thread=True, expect_stopped=False) +@capability("supportsCancelRequest") +def cancel(**args): + # If a 'cancel' request can actually be satisfied, it will be + # handled specially in the reader thread. However, in order to + # construct a proper response, the request is also added to the + # command queue and so ends up here. Additionally, the spec says: + # The cancel request may return an error if it could not cancel + # an operation but a client should refrain from presenting this + # error to end users. + # ... which gdb takes to mean that it is fine for all cancel + # requests to report success. + return None diff --git a/gdb/testsuite/gdb.dap/pause.exp b/gdb/testsuite/gdb.dap/pause.exp index 1b65dcac836..0f2fcbc6b2d 100644 --- a/gdb/testsuite/gdb.dap/pause.exp +++ b/gdb/testsuite/gdb.dap/pause.exp @@ -75,4 +75,75 @@ foreach event [lindex $result 1] { } gdb_assert {$seen == "pass"} "continue event from inferior call" +# +# Test that a repl evaluation that causes a continue can be canceled. +# + +set cont_id [dap_send_request evaluate \ + {o expression [s continue] context [s repl]}] +dap_wait_for_event_and_check "continued" continued + +set cancel_id [dap_send_request cancel \ + [format {o requestId [i %d]} $cont_id]] + +# The stop event will come before any responses to the requests. +dap_wait_for_event_and_check "stopped by cancel" stopped + +# Now we can wait for the 'continue' request to complete, and then the +# 'cancel' request. +dap_read_response evaluate $cont_id +dap_read_response cancel $cancel_id + +# +# Test that a repl evaluation of a long-running gdb command (that does +# not continue the inferior) can be canceled. +# + +proc write_file {suffix contents} { + global testfile + + set gdbfile [standard_output_file ${testfile}.$suffix] + set ofd [open $gdbfile w] + puts $ofd $contents + close $ofd + return $gdbfile +} + +set gdbfile [write_file gdb "set \$x = 0\nwhile 1\nset \$x = \$x + 1\nend"] +set cont_id [dap_send_request evaluate \ + [format {o expression [s "source %s"] context [s repl]} \ + $gdbfile]] + +# Wait a little to try to ensure the command is running. +sleep 0.2 +set cancel_id [dap_send_request cancel \ + [format {o requestId [i %d]} $cont_id]] + +set info [lindex [dap_read_response evaluate $cont_id] 0] +gdb_assert {[dict get $info success] == "false"} "gdb command failed" +gdb_assert {[dict get $info message] == "cancelled"} "gdb command canceled" + +dap_read_response cancel $cancel_id + +# +# Test that a repl evaluation of a long-running Python command (that +# does not continue the inferior) can be canceled. +# + +write_file py "while True:\n pass" +set cont_id [dap_send_request evaluate \ + [format {o expression [s "source %s"] context [s repl]} \ + $gdbfile]] + +# Wait a little to try to ensure the command is running. +sleep 0.2 +set cancel_id [dap_send_request cancel \ + [format {o requestId [i %d]} $cont_id]] + +set info [lindex [dap_read_response evaluate $cont_id] 0] +gdb_assert {[dict get $info success] == "false"} "python command failed" +gdb_assert {[dict get $info message] == "cancelled"} "python command canceled" + +dap_read_response cancel $cancel_id + dap_shutdown