From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id A433A3858439 for ; Wed, 10 May 2023 12:39:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A433A3858439 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683722350; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=CCxH9HX+KaV9OZ5f6jNWeUmHgT97wTz+1lq3HDJVIrk=; b=OaCrxQW7WxL/HddGZnVwdBahtWV28xuljYXJcAh1bjqUH2BEcUcFI2pV3gVh3sRahUOtCt CqAKcz7BR9GBfAzCeZNPZiYjak2h0iaepFIlBKBMAYYddWtI2qYpvkVHpfH7XWEzsxrVOR cRadtEFCMuQ7FvtAh+g+IGDr/f4F2k4= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-604-qZBksMKMOem9VgvsG_Neww-1; Wed, 10 May 2023 08:39:09 -0400 X-MC-Unique: qZBksMKMOem9VgvsG_Neww-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-3f315735edeso160410655e9.1 for ; Wed, 10 May 2023 05:39:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683722348; x=1686314348; h=mime-version:message-id:date:references:in-reply-to:subject:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=CCxH9HX+KaV9OZ5f6jNWeUmHgT97wTz+1lq3HDJVIrk=; b=Bksq6DV6XYkq6uKU0IYGE3pr0KT5GUGD9nn0XukDl/1J8esKuClp5j0PxQ/p23Gu61 Vh+hCKlFIltill5UN51eh8dJhAYSv1CM+zYrogyIouIayZnBJD0UWgAtLsNW78HidWWm 21mqE+DhdQzaqD5r5ogkuD5C92VLguAXsuh6r88YgfI8+kEZcUgTcX+nBO2xhimfOw05 YQAUsE/WKkTBayqEFt56muxjGI+/MihNTj4oQOty6naFj/v9zuZfDKmFuX8LjrrUpRNX KvmIZpg8lCDLcB1eoVPousAOHXZjlSi+S5jmVVurFKZf0/wDyAyRhZGp6EQEzdLOFQTq iWIA== X-Gm-Message-State: AC+VfDxMiI5RTh5M9k+HASxixjQZi205IvamEByxryC8XKkTDtK6C3Q+ eXsFlE2sSkOg44R+PmOryXoCKgK3WwUdw6fWVOUX4rpRnnw/mCBx4hFOoTlcz2aXH54BPUtzI+m JWnEDhDLbkk/TucsEfoMALIRD4MbNAw== X-Received: by 2002:a1c:7213:0:b0:3f4:2610:5cc5 with SMTP id n19-20020a1c7213000000b003f426105cc5mr6611547wmc.5.1683722347795; Wed, 10 May 2023 05:39:07 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ58eojzoaqjGTRgHwoOrldtG1oE9/uyF4cyCVzeh165dmsjOM0Oj9p5CiTD1k1gY310m+oIVg== X-Received: by 2002:a1c:7213:0:b0:3f4:2610:5cc5 with SMTP id n19-20020a1c7213000000b003f426105cc5mr6611531wmc.5.1683722347365; Wed, 10 May 2023 05:39:07 -0700 (PDT) Received: from localhost (11.72.115.87.dyn.plus.net. [87.115.72.11]) by smtp.gmail.com with ESMTPSA id n23-20020a1c7217000000b003f4ad703aecsm1441341wmc.15.2023.05.10.05.39.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 May 2023 05:39:07 -0700 (PDT) From: Andrew Burgess To: Tom Tromey , gdb-patches@sourceware.org Subject: Re: [PATCH 2/2] Add "args" and "env" parameters to DAP launch request In-Reply-To: <20230509-dap-args-v1-2-16b5f0aa5cd6@adacore.com> References: <20230509-dap-args-v1-0-16b5f0aa5cd6@adacore.com> <20230509-dap-args-v1-2-16b5f0aa5cd6@adacore.com> Date: Wed, 10 May 2023 13:39:05 +0100 Message-ID: <87bkis7446.fsf@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,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: Tom Tromey via Gdb-patches writes: > 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. I haven't followed the DAP additions much, but the code changes in here look reasonable to me. Reviewed-By: Andrew Burgess Thanks, Andrew > --- > 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. > > -- > 2.40.0