From patchwork Tue May 9 16:02: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: 68985 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 EB8333857004 for ; Tue, 9 May 2023 16:03:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EB8333857004 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1683648181; bh=qnS58f8Ft3doIWIWs2hBegyN4iimrPkI9ykrf5LjUnc=; h=Date:Subject:References:In-Reply-To:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=p1yI6lCnCvgx9HA7tAsqAx9wM+BxuxpW3dNOubUgSBRHx2dnYfGNHkTMBFPPnZTPz 02jOu0IQBAhBJbUsEUzREslU6MQkMs9lYyJVSmo9B9ULTdqiq9a/2qfbk/V/PI6vPT jUKLBH0bKf2TI6vUQH6EZJgLBZiOqPeOIHPaHw4c= 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 6F8D9385B53E for ; Tue, 9 May 2023 16:02:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6F8D9385B53E Received: by mail-io1-xd36.google.com with SMTP id ca18e2360f4ac-7666121cf5dso134552039f.0 for ; Tue, 09 May 2023 09:02:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683648124; x=1686240124; 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=qnS58f8Ft3doIWIWs2hBegyN4iimrPkI9ykrf5LjUnc=; b=BEjGjESCSofUPF9ccmXiU1jJoIibqxI4wNqxzkWq3fnqHsWhNOqk8ItwPXP4W+3S5b qsURRxtAkITW+BVtdfwYTVm84G7uiT5txy5pW/mv2bLgdilTPD0zbr9V5D0wj9LgZsFY 7MGKfG6rKVsvZYKd6/3eSqm+ibzguGe8s8kd0l4+QS0yJuwBvN14S9a8e6ngaXkxw1ys Iu+dhsFxkTD7QJuHcKrAam+z6A7uuYIgaM3tXpOI/Kr1giLkVM+q74KwnHNvmE8eZDJj zjOORvG4Aad10eZdXsS5/H2TsHH796bviokTWxnC9CR2/FNpS7qXGL4uPRcetMe2sL3o EY7g== X-Gm-Message-State: AC+VfDyPyC7t13bXIHyysXNH4Wl7b5X+QJM6qNdc8KYeQJvaCcPCtT1n yH8P4STzdV9fA0GAwJ6ndeiCn4JvCkPnnTlDNYw8+A== X-Google-Smtp-Source: ACHHUZ6atBpQ4xKlCHU++yfdKV7bM1DBTT/dIUQIGH0mY8tcPTD743/hhmgLeLyFveHN204TCCdK7g== X-Received: by 2002:a5d:9716:0:b0:76c:615c:7c1 with SMTP id h22-20020a5d9716000000b0076c615c07c1mr1191763iol.16.1683648124149; Tue, 09 May 2023 09:02:04 -0700 (PDT) Received: from localhost.localdomain (71-211-191-82.hlrn.qwest.net. [71.211.191.82]) by smtp.gmail.com with ESMTPSA id n8-20020a5e8c08000000b0076978505ea8sm2947021ioj.20.2023.05.09.09.02.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 09:02:02 -0700 (PDT) Date: Tue, 09 May 2023 10:02:00 -0600 Subject: [PATCH 2/2] Add "args" and "env" parameters to DAP launch request MIME-Version: 1.0 Message-Id: <20230509-dap-args-v1-2-16b5f0aa5cd6@adacore.com> References: <20230509-dap-args-v1-0-16b5f0aa5cd6@adacore.com> In-Reply-To: <20230509-dap-args-v1-0-16b5f0aa5cd6@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-11.4 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" This patch augments the DAP launch request with some optional new parameters that let the client control the command-line arguments and the environment of the inferior. Reviewed-By: Eli Zaretskii Reviewed-By: Andrew Burgess --- gdb/doc/gdb.texinfo | 13 +++++- gdb/python/lib/gdb/dap/launch.py | 20 ++++++++- gdb/testsuite/gdb.dap/args-env.c | 28 ++++++++++++ gdb/testsuite/gdb.dap/args-env.exp | 90 ++++++++++++++++++++++++++++++++++++++ gdb/testsuite/lib/dap-support.exp | 39 +++++++++++++---- 5 files changed, 178 insertions(+), 12 deletions(-) diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 8c4177c1901..343612060c6 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -38998,10 +38998,21 @@ Generally, @value{GDBN} implements the Debugger Adapter Protocol as written. However, in some cases, extensions are either needed or even expected. -@value{GDBN} defines a parameter that can be passed to the +@value{GDBN} defines some parameters that can be passed to the @code{launch} request: @table @code +@item args +If provided, this should be an array of strings. These strings are +provided as command-line arguments to the inferior, as if by +@code{set args}. @xref{Arguments}. + +@item env +If provided, this should be an object where each value is a string. +The environment of the inferior will be set to exactly as passed in, +as if by a sequence of invocations of @code{set environment} and +@code{unset environment}. @xref{Environment}. + @item program If provided, this is a string that specifies the program to use. This corresponds to the @code{file} command. @xref{Files}. diff --git a/gdb/python/lib/gdb/dap/launch.py b/gdb/python/lib/gdb/dap/launch.py index b4102cc28cc..21499a339e1 100644 --- a/gdb/python/lib/gdb/dap/launch.py +++ b/gdb/python/lib/gdb/dap/launch.py @@ -13,20 +13,36 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import gdb from .events import ExecutionInvoker from .server import request, capability -from .startup import send_gdb +from .startup import send_gdb, in_gdb_thread _program = None +@in_gdb_thread +def _set_args_env(args, env): + inf = gdb.selected_inferior() + inf.arguments = args + if env is not None: + inf.clear_env() + for name, value in env.items(): + inf.set_env(name, value) + + +# Any parameters here are necessarily extensions -- DAP requires this +# from implementations. Any additions or changes here should be +# documented in the gdb manual. @request("launch") -def launch(*, program=None, **args): +def launch(*, program=None, args=[], env=None, **extra): if program is not None: global _program _program = program send_gdb(f"file {_program}") + if len(args) > 0 or env is not None: + send_gdb(lambda: _set_args_env(args, env)) @capability("supportsConfigurationDoneRequest") diff --git a/gdb/testsuite/gdb.dap/args-env.c b/gdb/testsuite/gdb.dap/args-env.c new file mode 100644 index 00000000000..bc7f1d4b38e --- /dev/null +++ b/gdb/testsuite/gdb.dap/args-env.c @@ -0,0 +1,28 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2023 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include +#include + +int +main (int argc, char *argv[]) +{ + const char *value = getenv ("DEI"); + const char *no_value = getenv ("NOSUCHVARIABLE"); + + return 0; /* BREAK */ +} diff --git a/gdb/testsuite/gdb.dap/args-env.exp b/gdb/testsuite/gdb.dap/args-env.exp new file mode 100644 index 00000000000..96fbb28d9ce --- /dev/null +++ b/gdb/testsuite/gdb.dap/args-env.exp @@ -0,0 +1,90 @@ +# Copyright 2023 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Test environment variables and command line arguments via DAP. + +require allow_dap_tests !use_gdb_stub + +load_lib dap-support.exp + +standard_testfile + +if {[build_executable ${testfile}.exp $testfile] == -1} { + return +} + +if {[dap_launch $testfile {a "b c"} {{DEI something}}] == ""} { + return +} + +set line [gdb_get_line_number "BREAK"] +set obj [dap_check_request_and_response "set breakpoint by line number" \ + setBreakpoints \ + [format {o source [o path [%s]] breakpoints [a [o line [i %d]]]} \ + [list s $srcfile] $line]] +set line_bpno [dap_get_breakpoint_number $obj] + +dap_check_request_and_response "start inferior" configurationDone +dap_wait_for_event_and_check "inferior started" thread "body reason" started + +dap_wait_for_event_and_check "stopped at line breakpoint" stopped \ + "body reason" breakpoint \ + "body hitBreakpointIds" $line_bpno + +set bt [lindex [dap_check_request_and_response "backtrace" stackTrace \ + {o threadId [i 1]}] \ + 0] +set frame_id [dict get [lindex [dict get $bt body stackFrames] 0] id] + +set obj [dap_check_request_and_response \ + "evaluate argc in function" \ + evaluate [format {o expression [s argc] frameId [i %s]} \ + $frame_id]] +dap_match_values "argc in function" [lindex $obj 0] \ + "body result" 3 + +set obj [dap_check_request_and_response \ + "evaluate first argument in function" \ + evaluate [format {o expression [s {argv[1]}] frameId [i %s]} \ + $frame_id]] +set val [dict get [lindex $obj 0] body result] +# This ends up with some extra quoting. +gdb_assert {[string first "\\\"a\\\"" $val] != -1} \ + "first argument in function" + +set obj [dap_check_request_and_response \ + "evaluate second argument in function" \ + evaluate [format {o expression [s {argv[2]}] frameId [i %s]} \ + $frame_id]] +set val [dict get [lindex $obj 0] body result] +# This ends up with some extra quoting. +gdb_assert {[string first "\\\"b c\\\"" $val] != -1} \ + "second argument in function" + +set obj [dap_check_request_and_response "evaluate value in function" \ + evaluate [format {o expression [s value] frameId [i %s]} \ + $frame_id]] +set val [dict get [lindex $obj 0] body result] +# This ends up with some extra quoting. +gdb_assert {[string first "\\\"something\\\"" $val] != -1} \ + "value in function" + +set obj [dap_check_request_and_response "evaluate no_value in function" \ + evaluate [format {o expression [s no_value] frameId [i %s]} \ + $frame_id]] +dap_match_values "no_value in function" [lindex $obj 0] \ + "body result" 0 + +dap_shutdown diff --git a/gdb/testsuite/lib/dap-support.exp b/gdb/testsuite/lib/dap-support.exp index 6bb9b6e6377..ead295bdbfe 100644 --- a/gdb/testsuite/lib/dap-support.exp +++ b/gdb/testsuite/lib/dap-support.exp @@ -236,17 +236,38 @@ proc _dap_initialize {name} { # Start gdb, send a DAP initialize request, and then a launch request # specifying FILE as the program to use for the inferior. Returns the # empty string on failure, or the response object from the launch -# request. After this is called, gdb will be ready to accept -# breakpoint requests. NAME is used as the test name. It has a -# reasonable default but can be overridden in case a test needs to -# launch gdb more than once. -proc dap_launch {file {name startup}} { - if {[_dap_initialize "$name - initialize"] == ""} { +# request. If specified, ARGS is a list of command-line arguments, +# and ENV is a list of pairs of the form {VAR VALUE} that is used to +# populate the inferior's environment. After this is called, gdb will +# be ready to accept breakpoint requests. +proc dap_launch {file {args {}} {env {}}} { + if {[_dap_initialize "startup - initialize"] == ""} { return "" } - return [dap_check_request_and_response "$name - launch" launch \ - [format {o program [%s]} \ - [list s [standard_output_file $file]]]] + set params "o program" + append params " [format {[%s]} [list s [standard_output_file $file]]]" + + if {[llength $args] > 0} { + append params " args" + set arglist "" + foreach arg $args { + append arglist " \[s [list $arg]\]" + } + append params " \[a $arglist\]" + } + + if {[llength $env] > 0} { + append params " env" + set envlist "" + foreach pair $env { + lassign $pair var value + append envlist " $var" + append envlist " [format {[%s]} [list s $value]]" + } + append params " \[o $envlist\]" + } + + return [dap_check_request_and_response "startup - launch" launch $params] } # Cleanly shut down gdb. NAME is used as the test name.