From patchwork Fri Dec 1 15:40:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 81123 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 926B63861825 for ; Fri, 1 Dec 2023 15:41:19 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd32.google.com (mail-io1-xd32.google.com [IPv6:2607:f8b0:4864:20::d32]) by sourceware.org (Postfix) with ESMTPS id AD8F7385DC0D for ; Fri, 1 Dec 2023 15:41:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AD8F7385DC0D 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 AD8F7385DC0D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::d32 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701445263; cv=none; b=YA+YHu2WL4YoccjqeSWEqaPgEqsanWWewNDQ41d4JKTvFSAcAwGAF63paYzew8XMcvEQ/AkVPxTy92MyaJdnVgYGbKJw0QbzKCFz9Qe8DzUqjhFRF5qtmbOtoDizgg/DYoUO5B6hp+PdOkW0oDFuNZ/xbehsYJeA6yGFHpvYvWo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701445263; c=relaxed/simple; bh=tQ4OLopZ2FaFLpwlh4JAOhsGUBBESGhdzLUeZ6z98NI=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=KSIZAQRXqBhmZKY2+/I4fpCgR6QJOX+kA53AwG9Wyc1LCuaWiStwe0ec6atSiWSyakibUub1OOZ42csZSgbHLZekLWtaGrW9KQGfif7A7IdYfa4auFkIxJ6e++GpBmJCWTC4KkP/PB2SpGY2fV5nyldl8Fr6qGTPuWe9bDSDDDU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-io1-xd32.google.com with SMTP id ca18e2360f4ac-7b38a2db18aso69133139f.1 for ; Fri, 01 Dec 2023 07:41:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1701445260; x=1702050060; 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=ZuM/IEtMBeeR1G193sEDacsh4B4ivTD5KZADJpvn2LU=; b=PGHD4nUi/SYH7moca89h8HIrfA2Mv14kmQ3NZ0r68PrYb2Bw78GjHtTAizAHyz979v /rKbsuodc9B0SHmBDZ/KPxZXD1l6T2V8erSrydLCqDNBFGDxg26kdvsy+Ged/gt/1et2 z4QgmyVl79SzX1fUpy7sarXG98nnWDF1T7Hg4nU0aSpwy/e9BdyvrGFqdzuu2ITYQP4A ueLTmkeY9l7qS1v5z83RcLRwN2lDV97KMx50arL+q/ShKvctE3tEGh9qZYJvChFu2OyL 4rAox5379Z9qMqmUp1qM3XDq/SX/XGVt1QYvaEmW9TByxFG5GVBGvTSodHk3p7/ZKAJE DZmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701445260; x=1702050060; 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=ZuM/IEtMBeeR1G193sEDacsh4B4ivTD5KZADJpvn2LU=; b=RZOcWDyv4GZKGxHKl7SrCd1Il9gl7gaczMC8SuvXUs739x6tB7GelqbA7rbux0KBTS M7AuWERsFBIZCMuI1JTPv3ZHtIGrZvMzM0dvSpWlWlNypz+EnIzLlFWGkDPQBILnrJSQ n0Gn4WzvuSlUTfDCu4f2xZcs5EeB4GnR4VDyFu/PdkYuMyF+dTARx6RSPJZxfggn9DTC eb089s8gwI/rYVIg2bm1a6+MF3FbV/GisxVTvdNpuA2Z0OtCG1dcdoInWUe0AOcCeAI9 asL0HLMMh/fK+EjMJO/6LV87JlT+cdfTSJaigyjOqujWtIYW59IaKNZS0qB2fwCpQcGh Yx7A== X-Gm-Message-State: AOJu0YxG8AcB5irjKlXv0uFhLrctgarlAUZDMUwSPQWHZUlzEWyk00Yq 3B4owo00K7oKbZASFTMkye8f0od4Ibwak3zm8IQ= X-Google-Smtp-Source: AGHT+IEigQhqTbCsKdoFUzhM5V45T9vlQKNLpK+wvLwQls7kdQUhIJZSGBpewJZWY7Rx3/OIzeHVDA== X-Received: by 2002:a05:6602:341d:b0:7b3:5ac0:3a58 with SMTP id n29-20020a056602341d00b007b35ac03a58mr31948853ioz.10.1701445260433; Fri, 01 Dec 2023 07:41:00 -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.40.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 07:41:00 -0800 (PST) From: Tom Tromey Date: Fri, 01 Dec 2023 08:40:57 -0700 Subject: [PATCH 1/6] Clean up handling of DAP not-stopped response MIME-Version: 1.0 Message-Id: <20231201-dap-cancel-v1-1-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 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. --- 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 Fri Dec 1 15:40:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 81125 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 822103861844 for ; Fri, 1 Dec 2023 15:41:35 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd36.google.com (mail-io1-xd36.google.com [IPv6:2607:f8b0:4864:20::d36]) by sourceware.org (Postfix) with ESMTPS id EB825385E004 for ; Fri, 1 Dec 2023 15:41:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EB825385E004 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 EB825385E004 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::d36 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701445264; cv=none; b=oOuMI6zGg28RkQIsCRO6ICnOWx/Zz/Sc0Qx9Y9r7oSFctlBAiJBOMO3wwV9/enuHf6m7DE2phJI8XWeFQFbuoynlPGLo7ZXyXC7e8cWhTtIl7Nvbz3uPg4BkWPb52x9l2MM4nJAKgmE/hSRNspLRFmYwNn2CsH7A0lEvVsiV1Y4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701445264; c=relaxed/simple; bh=DZE83WupOVHeNfNXTshoKEFQtuccJkw/tCqHhVVoQqU=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=A+hZNdK7pU8wXhsU6kU/DiDByYhu9sPQTHiENe0CQZKiEMrwMlSBvJuagxx1Io9PAc+KmqbqIrBsH7H3yKAMUwsHRFDyWIkCdZvzY+0P/QboRpI9IvAHtdv9iK9Gfn1j0iwNY2tPZjjRgYpVEesBD1mhRJFy3W609YTsVwXmZHo= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-io1-xd36.google.com with SMTP id ca18e2360f4ac-7b34ec3e44fso65621839f.3 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=uoBckuW4XBZE9Y2w8S/O7r3WHv1x/M86rcm35qcwzl8=; b=Oedt7kYL8ZN1XP5DJUAqcZU2PuXdhepBse1qj2PPZbJBTTMFGoj27eo+ER5R0gLASl x3ejxdyKC/OIwuwytBsU3M6vyiTklEENDzJ3BXXjRVNKtgRkTfalt5Qg01TZcia9XkUT odcezqf5WrOEd/HGQDeMsk9eciSi7s8wbPgZtkOQuTIQ/npBatbu9xoJvW0fzwE5XNVo dRupe3zZBdlhA7uuIsJ6uXhvldkYV+Rx1Pc2kqQk9QLt9gM4QqDtu0DsSjZospAzpfSK p1u73TY1D4zEJu4uAecN6YThhquSkdbg/UqlHWiQc1uO1f0Jx6QPn1ZMqy7AoQ9n4kuW Nehw== 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=uoBckuW4XBZE9Y2w8S/O7r3WHv1x/M86rcm35qcwzl8=; b=ucqw+RziDaim58FqcXkXTAiJIWr+PZy2D3bwf02CXC/cI7/J02aIxcKOPw+7m+v0J7 tODExBstO0r6+lewLy6/YfGLmNc7GIWFl3KuPlD6eM2Wqsq5uUnpj9oTgkwGmqBOccLE T2wcJ+A7B0HHmzb41HKYFZGfK6fFGt0svKjtdPl00mD11HGE28xD2WMh7MG2wPzoi+Tl 53elZWc1hdfdxrzd4A+HiHBFfuZ+vx2Wr5WSpwzuGz9XYj0Oz4iUTJM0ZWw2aH5dTG6w FYPB19CxJi1L7RjI80jU3RqNXfDyE3elpS4mVERB8KfebbMlGtW1ufG0kOYbJCfEZ84O mxiw== X-Gm-Message-State: AOJu0YzfnuLIOA+uZtB4gK2BbTM5OMdVnwaFw47nqMU8WQLLP8uQm7Y9 acdFoAgUizdJvyMZghO6A3iL+C0tlwU/sp0vqsQ= X-Google-Smtp-Source: AGHT+IE0wIDnkEK7qMs6MikpzC6OxTn7iCzSaQBs5JZg/SnFS23j1ou5T9zMSwPwBva2P7UN4hEicQ== X-Received: by 2002:a6b:7506:0:b0:7b3:f55c:49a7 with SMTP id l6-20020a6b7506000000b007b3f55c49a7mr7653679ioh.3.1701445260986; Fri, 01 Dec 2023 07:41:00 -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.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 07:41:00 -0800 (PST) From: Tom Tromey Date: Fri, 01 Dec 2023 08:40:58 -0700 Subject: [PATCH 2/6] Move DAP JSON reader to its own thread MIME-Version: 1.0 Message-Id: <20231201-dap-cancel-v1-2-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 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. --- 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 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\ From patchwork Fri Dec 1 15:41:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 81126 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 3F0F8386186F for ; Fri, 1 Dec 2023 15:41:39 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x12e.google.com (mail-il1-x12e.google.com [IPv6:2607:f8b0:4864:20::12e]) by sourceware.org (Postfix) with ESMTPS id 1B57C385E032 for ; Fri, 1 Dec 2023 15:41:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1B57C385E032 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 1B57C385E032 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::12e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701445264; cv=none; b=vS6ucYWznciXeLm5TbfviRMvpFJ60nG4VBVdBezAQm5tjMfAzkejEAAeFdeDujgrM13qOLTCCy+0Rz0rdsu7bEtMp2aQDRsFcFhcFxl+qWRK09FZpjkBAZILvuKuc5aWAXtf3t60/EHuEJVfGQd9nkLK0TtvguprVRD6ih4oh94= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701445264; c=relaxed/simple; bh=CSvb6Mbu7or+IsTbdtOI1xeakIwYL9/03CISo/RguIY=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=r4OwNtwPJ4qZGVP8KivabfooV+bl1EZzHtWzw5stpiVdWgixL7LXVfmfF02yHuyGPbP1MO3CktRyGDiV80ls1cI4QJwKawI4uiKymq37VVz4Yzb/aERyGfD8KIS2hEfLeSCAucjd3lCyABW17fnJBcVpTOQaur+lEpU3oTzu1tQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-il1-x12e.google.com with SMTP id e9e14a558f8ab-35cb62e3905so7273595ab.3 for ; Fri, 01 Dec 2023 07:41:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1701445262; x=1702050062; 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=mLGy+z8F3pKUx+O2IjrxHWPXgsv58vyysN5FyXcNdck=; b=VA8or02Mx8yViYRL8OoXkPwQ0rVKdIWeBMMdOXS/F2LMtLWnISLJ38cD7zwYlUxhzq Q4OFMhb3EXkNPfPOXWNiNC5zsT8qMAP6rybGa8JM0a14qaWHxL+RDe+DWTy4ctlgUirB veYLyqxL9sgQT6bRjehrP6dXRWHBZPl4Tz0EVdxI+aVaYnURnZdT/AcusLUzlgN1z4Jf KPPlEVeelYNhR26zMt3KlTo1VI/4GdcA3sasP8FX0Dx4WvKtjgJG340SgYBGJB0Hj4is UYNKdvyPLFNl2b3Pd+GyMuvkbB7Wca1jpMhIMHbWJo7d8a6h6CwMy4PTywnc7DHRuFT0 jTUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701445262; x=1702050062; 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=mLGy+z8F3pKUx+O2IjrxHWPXgsv58vyysN5FyXcNdck=; b=L62W86JLHy2WWDvZNOASVtK2ZwBYCiPpDQyNAxf9Fu0oPnfWgYqwbXsjiApCJj4HpG Gu5l8vPPBc9otQEWo14nrlx5gmtPEjgT5LcgFEgghO2buwkQWOrTYpa4vOomZR8j9RTS rsKcT21ThY35SaXY9hfTnrmhWbdNe8e+66R0vOqLhjNHxvgmHmGFIPVQq/+F2EuEkBSW VdgGgaY0RsxI74/G8DAK1O+6wGVv5eV1Yrbg2yz35clIYe0u20vgWfqdCBVpLkFlH/lw 0oDrTUuZSlgYMP9BggiIR5jyvJSyKXd81BNCvUGfe1U91B8RIp+/I8OefYp7cf0f1cay KYEw== X-Gm-Message-State: AOJu0YyM3VOvmUiZY2v0xBoBYPAK+/RHbVD4iK2BGQ6dvyqG7COFdcke TJK5ccktAnuW4EJnpuLu/adXIZNJY2mNZv01l0o= X-Google-Smtp-Source: AGHT+IFSiXUKBa4UL9oQMOy7BybgyKBUg3njwZZV28AMFNiz5plBGw8NZizvQu8xDS88onxl0XNXTQ== X-Received: by 2002:a05:6e02:2193:b0:359:6e32:70ae with SMTP id j19-20020a056e02219300b003596e3270aemr31931977ila.4.1701445262247; Fri, 01 Dec 2023 07:41:02 -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:41:00 -0700 Subject: [PATCH 4/6] Rename a couple of DAP procs in the testsuite MIME-Version: 1.0 Message-Id: <20231201-dap-cancel-v1-4-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 This renames a couple of DAP procs in the testsuite, to clarify that they are now exported. The cancellation test will need these. --- 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 Fri Dec 1 15:41:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 81127 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 49824385C41D for ; Fri, 1 Dec 2023 15:41:45 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x12c.google.com (mail-il1-x12c.google.com [IPv6:2607:f8b0:4864:20::12c]) by sourceware.org (Postfix) with ESMTPS id A0362385DC33 for ; Fri, 1 Dec 2023 15:41:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A0362385DC33 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 A0362385DC33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::12c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701445264; cv=none; b=xVuMYhlO/FhdarQD++B/9+CWioNVbugSkGUAS2DHiEJg0YmHlIw/39TZkxU5v0HT5dUGa91W22AWCEaUH/407hDJPSNvW+Npz7va5oO8JzUR27Zw6ulXw+t45AGLLOI43qo/RExY8+ceYkM1CwtQW4tGy4beuoEuiHbYLDsFBp0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701445264; c=relaxed/simple; bh=ygcqaVDGAh5aMmjlgvc3s2HB4ixcidCTj8zmQsK71QI=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=nuXcDQOhMm5qCtGeI2Ll4AsybJWggKkvlYvbtvGS8d+jjXtEt56EHmJDArY8iC/BixwSoMB1GEV5vvP2NeBmnKB6of93BfWNT+74bogm5tEhoq5lHAFweGzhi74yyb1lIwZYjscgY6wCvyZ8IExysMA2Q9q0/w7YeK2WXlDnxiM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-il1-x12c.google.com with SMTP id e9e14a558f8ab-35d4de84195so2890695ab.3 for ; Fri, 01 Dec 2023 07:41:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1701445263; x=1702050063; 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=22TNBWx7bPvyA7PnYRekI1hZSkV/8+P+ayq1trU/JxA=; b=ZHktgTUZ3IVOYQZtn7zhIejbXYzRyX/Hti1QThCFk2ZmWoAc+u7BbEmp3svG7E9Go4 iqNGxyQRgXI98hLkY7pKs1F3eSZiPHZNmiGlLeyBdxgZkYz1htMbr6OcfksCF40kJxIb C9YyVhEA8Pom99xMJuRtbKWadJ2d9kYXa9DDZR5P8VtvqFfyLh1/wyxYNlNcG9bASbUg atAGqnmoaRhyV+UsgqzoZ6S3oAQufRo1Ykn2hm5PqBVltd47OneclExwoDyIusCyizFx OOeV/keR9O0VXXDut8AgQ9LGo51s+AWmG5k8ussykjpfNk3fdVdj1Uh/21WzlfcGpvbm +kAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701445263; x=1702050063; 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=22TNBWx7bPvyA7PnYRekI1hZSkV/8+P+ayq1trU/JxA=; b=TpIri3DCcwnCn4iS1FF7gHTJtidyxj5gXneH6f3wzvrgrbB5wHxfJpF/bNFQrlnzP+ 75Bjf9S2mrPv4nP9mkBNlv93IaKrbuQZiq0M0p3EYrqixrs5n5NKWLzgF7xxnWmeDstV 4M1pkONOST/pXraY+C0m2sAntFlD4r8vn48VOdeoBFx0jq0YNFADsaDXkn8JhDa16rGJ sddzFpaEgYp0+Fqz6nIZ90AqkFiH11eQ2In9KtjMiCj9QB8g0Kg5JArHT5mawV5JbSNs cvGJNzK4tKuSROvCiiNjjGO2NRpBHAwYVDFOwXEKyQwkfLmq7VOPft/PqSSGe+F12Tx0 jPLw== X-Gm-Message-State: AOJu0YxcaGGi6I+JXwjPmTXA3iGZ3ShR0GXotu1FEVNAu/hMBYY1150j 6kTC17m8nHidKcgq1vvIL3Fx1NY0j2+OrW0CzFg= X-Google-Smtp-Source: AGHT+IFANphkEp6t+iYxO8feoBxmwWbevgJFA4V/spnAKve4/XfwHVnKCRSfTfIXgz6JqjLGV0fqHA== X-Received: by 2002:a05:6e02:214b:b0:35d:37de:ca2e with SMTP id d11-20020a056e02214b00b0035d37deca2emr6800282ilv.0.1701445262829; Fri, 01 Dec 2023 07:41:02 -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.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 07:41:02 -0800 (PST) From: Tom Tromey Date: Fri, 01 Dec 2023 08:41:01 -0700 Subject: [PATCH 5/6] Catch BaseException in send_gdb_with_response MIME-Version: 1.0 Message-Id: <20231201-dap-cancel-v1-5-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 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. --- 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..1a7ec17e5e3 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 BaseException as e: result_q.put(e) send_gdb(message) val = result_q.get() - if isinstance(val, Exception): + if isinstance(val, BaseException): raise val return val From patchwork Fri Dec 1 15:41:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 81128 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 2A3EE385E45D for ; Fri, 1 Dec 2023 15:41:56 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x12c.google.com (mail-il1-x12c.google.com [IPv6:2607:f8b0:4864:20::12c]) by sourceware.org (Postfix) with ESMTPS id C7DFE385E03D for ; Fri, 1 Dec 2023 15:41:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C7DFE385E03D 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 C7DFE385E03D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::12c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701445267; cv=none; b=F8UwBmoDYhvoVADrX6/7ZK/r04EnN7e/YEZNo2WS0X8nyll1X4w35I/syS2jz4SKdR8paJGbMBbQAtbdVpwl4MT+8JElQmjv7hPkuoIwbH8l2jUD25wecHHnG9mWs+FPrFQ+V4y/Iy+qhZuwZuby6qWdopzNmCi+RGGZC6i1bSE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701445267; c=relaxed/simple; bh=WgJ9DQ1L2gLTFfvqqqTeWPeAZDWXumUpErOamgGTRIM=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=n6aSDqlp+kec6prCdJpE9y46tuApP2TwVvnLy8dlwDNKLqxeAsBFVj9FZRhu05G0hAUxcntoG6xLtlUeTcfP4pM1SS1v0FoEVrimvp10wLgcL39MQXO8x34OveiFhT68cMnGGjAkFT7LU74NcWaZn5BqDI4wZ7NSDZwFcfYuiv0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-il1-x12c.google.com with SMTP id e9e14a558f8ab-35d3b220b64so7123175ab.2 for ; Fri, 01 Dec 2023 07:41:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1701445264; x=1702050064; 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=DXCVO7ae5YLjInU7nFq5uVJ7Mll2QDJQhQ+LQbADjUQ=; b=SWuIe+zsLp6orHsbjLP2PO6MRwUdPMbZZDMi4K9rwiNc2wzpn4RJh4aERH3jL4IFno 6eckzK/qzaIcUfPl8BdIixBf/nfpoGcum0xeB8WCKBdr79G5eEUx6rZjWQMOgf3ltOFn Ao8/RlEzCflot/kfxyGFncH961H5sJt21g3PL0rHb4gqKqwoVff9azsLzQGwjGWRMRe5 nNlkdAvENl/GEuJFlFvH/NpfCbN/Bn0KvFQQaGJxXoNYGrfsXwfXKyeg1kDtTX7Jj+Yj Gu0Z7Wdyauk2BOqLhONsK9EX2uVRCrzFXvoovtrOjDnOWQPrDwKYwK3K7EWX11oTQoTr HjKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701445264; x=1702050064; 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=DXCVO7ae5YLjInU7nFq5uVJ7Mll2QDJQhQ+LQbADjUQ=; b=q8G/lRVh8lRiEFHWZSUG0EXELNdsmZ0YuUcMZ2GTqaJVTE37QA3BK8Z2J5vw7tkMZH 8Z2w0BpTmSxHgJj6XtCp5wgG5bgqHksbqtJG+zl7EWZ7o1uzqH+zdpQ/ZZE8sO+Hxw+p 5mVFWHvJP/KPB/kOmE8bd2xcTcK2Vo+956d09+uN82TzyC9LuAUoojAQcVDJUCjrgzPQ dlaDMJhq8mYb816vmHMYa1p56BDXAQ1uY1cY/lVOwsaOP73B1W05EEb0uyM1VMVZo86j 3oIQMXAwIoy4AkbECO9H9kCIrAA/YJtmH6PsgLX31YYaaEWYbd0sGkkxQiycwKX6yfwk AYaA== X-Gm-Message-State: AOJu0YwM8fnCwBrMsFp4VCD5ob5D6sojvizO1aNu4l8S3nJjiUVN3JMI MEEQ5psvn2uQ76NVRR9BTvKo0b/VdKVb73vmELQ= X-Google-Smtp-Source: AGHT+IG9T3gXO4usNx2ppHGsLWPJYU3xKhTOXdABNgLizvJr6tfPv77BERDgj6rt5i7+NTj5vrJfmQ== X-Received: by 2002:a05:6e02:106e:b0:35d:47b1:b656 with SMTP id q14-20020a056e02106e00b0035d47b1b656mr3496681ilj.6.1701445263890; Fri, 01 Dec 2023 07:41:03 -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.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 07:41:03 -0800 (PST) From: Tom Tromey Date: Fri, 01 Dec 2023 08:41:02 -0700 Subject: [PATCH 6/6] Implement DAP cancellation MIME-Version: 1.0 Message-Id: <20231201-dap-cancel-v1-6-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, KAM_SHORT, 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 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 Reviewed-By: Eli Zaretskii --- gdb/doc/gdb.texinfo | 16 +++++++ gdb/python/lib/gdb/dap/server.py | 91 ++++++++++++++++++++++++++++++++++++++-- gdb/testsuite/gdb.dap/pause.exp | 71 +++++++++++++++++++++++++++++++ 3 files changed, 175 insertions(+), 3 deletions(-) diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index e4c00143fd1..c7c56f2e967 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -39541,6 +39541,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