From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-il1-x12d.google.com (mail-il1-x12d.google.com [IPv6:2607:f8b0:4864:20::12d]) by sourceware.org (Postfix) with ESMTPS id DEDF73858C01 for ; Fri, 10 Nov 2023 15:14:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DEDF73858C01 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 DEDF73858C01 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::12d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699629296; cv=none; b=mEyEX6qD5XopMjCmFoNTl0mUWFwje735R2FcHC27lHem1ta0W+twwwaXHn5gjJ/YphFFXveGEHODL0ZMAc+dPgLvwMS7egwkAnLSr594pTCPkCHauW/jk23JDrUWz+o2+hLoV3nbYtD6P6jfa6daJ6t8c2XI0qJcuijIN3uH80Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699629296; c=relaxed/simple; bh=0TN5fdVSwDw1cqek65ZizpX2jhWzV8q04dYGx3o6bPg=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=jIsOKa7OwTWiSQaDgMjvRG2b3LUQ3OLEr2v7+zb05cVq5aAMa7hpdl5KjwdGsgMB+kWPmbNswdy/FWUaMLXaEk0MN0E3Iml9gOvybvqIwSWK27JXZCGWMWJT7iXFZI1B2KggkW6WUo7qQVIklP0XS4SaevuLE1vQ5+ohU1aN/pM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-il1-x12d.google.com with SMTP id e9e14a558f8ab-3594c100735so5393105ab.1 for ; Fri, 10 Nov 2023 07:14:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1699629294; x=1700234094; 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=3g5CDvfWVT2PIYOMn8nd/1Q8zqm1qHy+hPjzU8PVxPM=; b=Xl+f17WKbpQbJTMaM3Ej6u58VwhHxJaZW5UmrjIMyKUvwa6BtP/30YrYxiBffMAH0L fjqO99FFj8ZJrNMele8mMXeCsmA7HjcvfIgA4b2nwzzcuTJZaiIU25XyO6hAn+M+q0j3 CEK/gvCn2TCtdA0colD9VfbW35n+qt4txQo4HNR2cgmOBpcNxNVQCrqnaIl0x3srCfES CKo+XDWrAqf4v9VXguE5z5MEBUCMexBZBThwB7YG8KikmcDDzhn50a5PS1xD4qqb7pM4 VssZ8s2NsmSwHaecGmGjuoQJiw4e96D1jaa700GutFjuqbjIsv4JwsF7GmSQkvM2QYx0 Dnnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699629294; x=1700234094; 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=3g5CDvfWVT2PIYOMn8nd/1Q8zqm1qHy+hPjzU8PVxPM=; b=oKVt2LBBN9n4uRjdfl4gNAChcNho3H37X1jpQS6Q7nNW6vqIFTNAdYL7vEpzooNIlV ZlOYgMMC9TRVS6fawQIScja8K8n8foaZhaMQ08iWJBHJwDsGOgynKjF6X//6Fr0GFq0o ZOBfzRYgJvK2Z/806tgPhTYOp9t02XVUyDauymeUKRkZ8IMLDi6wnX2Bq1wLDqFuDh0f OGvysfWbaqC0bc+glxrj1lAGO9RVC1r5XX5z7jLhHnr12+rUWWMGZeDmPFPwGExPuNqP NLmoysuupbyS8JMCI2X8YGKLAdA0887Q2y8srNp5xFIz6w0kH494iprdkR+KoJnLVu/q MnGg== X-Gm-Message-State: AOJu0YwDLeWxX1jXAAbHk2JICfDhGSaIpVi8MWC5EX/aFOnUK8Gnc/bs SIlATLEpBRiSRCuKadRUeG3xYHA4V++L5sKh7i3lHA== X-Google-Smtp-Source: AGHT+IE7Wh8BLwMr6Cd2mG6BHf1tOPtoPuuRkk9jBeh6GS/3ZHAh+nNpnsw28pXCIl8xUR/QMBrxAg== X-Received: by 2002:a05:6e02:19c6:b0:359:315c:368b with SMTP id r6-20020a056e0219c600b00359315c368bmr3065478ill.4.1699629294030; Fri, 10 Nov 2023 07:14:54 -0800 (PST) Received: from localhost.localdomain (97-122-77-73.hlrn.qwest.net. [97.122.77.73]) by smtp.gmail.com with ESMTPSA id m14-20020a0566380ace00b0043a11ec651asm3998196jab.169.2023.11.10.07.14.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Nov 2023 07:14:53 -0800 (PST) From: Tom Tromey Date: Fri, 10 Nov 2023 08:14:51 -0700 Subject: [PATCH v2 3/3] Implement the notStopped DAP response MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20231110-dap-not-stopped-v2-3-e2cadf3e29ba@adacore.com> References: <20231110-dap-not-stopped-v2-0-e2cadf3e29ba@adacore.com> In-Reply-To: <20231110-dap-not-stopped-v2-0-e2cadf3e29ba@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.4 X-Spam-Status: No, score=-11.9 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 List-Id: DAP specifies that a request can fail with the "notStopped" message if the inferior is running but the request requires that it first be stopped. This patch implements this for gdb. Most requests are assumed to require a stopped inferior, and the exceptions are noted by a new 'request' parameter. You may notice that the implementation is a bit racy. I think this is inherent -- unless the client waits for a stop event before sending a request, the request may be processed at any time relative to a stop. https://sourceware.org/bugzilla/show_bug.cgi?id=31037 --- gdb/python/lib/gdb/dap/events.py | 15 +++++++++++++++ gdb/python/lib/gdb/dap/pause.py | 2 +- gdb/python/lib/gdb/dap/server.py | 39 ++++++++++++++++++++++++++++++++++++--- gdb/testsuite/gdb.dap/pause.exp | 7 +++++++ 4 files changed, 59 insertions(+), 4 deletions(-) diff --git a/gdb/python/lib/gdb/dap/events.py b/gdb/python/lib/gdb/dap/events.py index 09214ec3dc8..bfc3f9ee1dc 100644 --- a/gdb/python/lib/gdb/dap/events.py +++ b/gdb/python/lib/gdb/dap/events.py @@ -21,8 +21,17 @@ from .startup import exec_and_log, in_gdb_thread, log from .modules import is_module, make_module +# True when the inferior is thought to be running, False otherwise. +# This may be accessed from any thread, which can be racy. However, +# this unimportant because this global is only used for the +# 'notStopped' response, which itself is inherently racy. +inferior_running = False + + @in_gdb_thread def _on_exit(event): + global inferior_running + inferior_running = False code = 0 if hasattr(event, "exit_code"): code = event.exit_code @@ -48,6 +57,8 @@ def thread_event(event, reason): @in_gdb_thread def _new_thread(event): + global inferior_running + inferior_running = True thread_event(event, "started") @@ -85,6 +96,8 @@ _suppress_cont = False @in_gdb_thread def _cont(event): + global inferior_running + inferior_running = True global _suppress_cont if _suppress_cont: log("_suppress_cont case") @@ -123,6 +136,8 @@ def exec_and_expect_stop(cmd, reason): @in_gdb_thread def _on_stop(event): + global inferior_running + inferior_running = False log("entering _on_stop: " + repr(event)) global _expected_stop obj = { diff --git a/gdb/python/lib/gdb/dap/pause.py b/gdb/python/lib/gdb/dap/pause.py index d276ab1cb92..b7e21452d69 100644 --- a/gdb/python/lib/gdb/dap/pause.py +++ b/gdb/python/lib/gdb/dap/pause.py @@ -17,6 +17,6 @@ from .events import StopKinds, exec_and_expect_stop from .server import request -@request("pause", response=False) +@request("pause", response=False, expect_stopped=False) def pause(**args): exec_and_expect_stop("interrupt -a", StopKinds.PAUSE) diff --git a/gdb/python/lib/gdb/dap/server.py b/gdb/python/lib/gdb/dap/server.py index 4430d2aba5f..031bf49f486 100644 --- a/gdb/python/lib/gdb/dap/server.py +++ b/gdb/python/lib/gdb/dap/server.py @@ -13,6 +13,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import functools import inspect import json import queue @@ -163,7 +164,28 @@ def send_event(event, body=None): _server.send_event(event, body) -def request(name: str, *, response: bool = True, on_dap_thread: bool = False): +# A helper decorator that checks whether the inferior is running. +def _check_not_running(func): + @functools.wraps(func) + def check(*args, **kwargs): + # Import this as late as possible. This is done to avoid + # circular imports. + from .events import inferior_running + + if inferior_running: + raise Exception("notStopped") + return func(*args, **kwargs) + + return check + + +def request( + name: str, + *, + response: bool = True, + on_dap_thread: bool = False, + expect_stopped: bool = True +): """A decorator for DAP requests. This registers the function as the implementation of the DAP @@ -178,6 +200,11 @@ def request(name: str, *, response: bool = True, on_dap_thread: bool = False): If ON_DAP_THREAD is True, the function will be invoked in the DAP thread. When ON_DAP_THREAD is True, RESPONSE may not be False. + + If EXPECT_STOPPED is True (the default), then the request will + fail with the 'notStopped' reason if it is processed while the + inferior is running. When EXPECT_STOPPED is False, the request + will proceed regardless of the inferior's state. """ # Validate the parameters. @@ -217,6 +244,12 @@ def request(name: str, *, response: bool = True, on_dap_thread: bool = False): cmd = non_sync_call + # If needed, check that the inferior is not running. This + # wrapping is done last, so the check is done first, before + # trying to dispatch the request to another thread. + if expect_stopped: + cmd = _check_not_running(cmd) + global _commands _commands[name] = cmd return cmd @@ -255,13 +288,13 @@ def initialize(**args): return _capabilities.copy() -@request("terminate") +@request("terminate", expect_stopped=False) @capability("supportsTerminateRequest") def terminate(**args): exec_and_log("kill") -@request("disconnect", on_dap_thread=True) +@request("disconnect", on_dap_thread=True, expect_stopped=False) @capability("supportTerminateDebuggee") def disconnect(*, terminateDebuggee: bool = False, **args): if terminateDebuggee: diff --git a/gdb/testsuite/gdb.dap/pause.exp b/gdb/testsuite/gdb.dap/pause.exp index 27955d31526..558ede982ee 100644 --- a/gdb/testsuite/gdb.dap/pause.exp +++ b/gdb/testsuite/gdb.dap/pause.exp @@ -32,6 +32,13 @@ if {[dap_launch $testfile] == ""} { dap_check_request_and_response "start inferior" configurationDone dap_wait_for_event_and_check "inferior started" thread "body reason" started +set resp [lindex [dap_request_and_response evaluate {o expression [s 23]}] \ + 0] +gdb_assert {[dict get $resp success] == "false"} \ + "evaluate failed while inferior executing" +gdb_assert {[dict get $resp message] == "notStopped"} \ + "evaluate issued notStopped" + dap_check_request_and_response pause pause \ {o threadId [i 1]} -- 2.41.0