[2/2] Add "args" and "env" parameters to DAP launch request
Commit Message
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.
---
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(-)
Comments
> Date: Tue, 09 May 2023 10:02:00 -0600
> From: Tom Tromey via Gdb-patches <gdb-patches@sourceware.org>
>
> 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.
> ---
> 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(-)
Thanks.
> +@item env
> +If provided, this should be an object where each value is a string.
This doesn't say what should those string values be. Shouldn't they
be of the format "@var{variable}=@var{value}"?
> +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}.
This could potentially confuse: if this is equivalent to "set
environment", then the variables are _added_ to the one inherited from
the parent process, no? Also, when would this be equivalent to "unset
environment"?
Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Tom Tromey via Gdb-patches <gdb-patches@sourceware.org> 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 <aburgess@redhat.com>
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 <http://www.gnu.org/licenses/>.
>
> +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 <http://www.gnu.org/licenses/>. */
> +
> +#include <stdio.h>
> +#include <stdlib.h>
> +
> +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 <http://www.gnu.org/licenses/>.
> +
> +# 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
>> +@item env
>> +If provided, this should be an object where each value is a string.
Eli> This doesn't say what should those string values be. Shouldn't they
Eli> be of the format "@var{variable}=@var{value}"?
In JSON an object is a dictionary; so the keys are strings and the
values are as well. That is it looks like {"variable": "value"}
Tom
> From: Tom Tromey <tromey@adacore.com>
> Cc: Tom Tromey <tromey@adacore.com>, gdb-patches@sourceware.org
> Date: Tue, 16 May 2023 13:06:09 -0600
>
> >> +@item env
> >> +If provided, this should be an object where each value is a string.
>
> Eli> This doesn't say what should those string values be. Shouldn't they
> Eli> be of the format "@var{variable}=@var{value}"?
>
> In JSON an object is a dictionary; so the keys are strings and the
> values are as well. That is it looks like {"variable": "value"}
That's okay, but I think we should say something about the structure
of those strings, and {"variable": "value"} goes a long way towards
that.
> Date: Tue, 16 May 2023 22:10:54 +0300
> Cc: gdb-patches@sourceware.org
> X-Spam-Status: No, score=1.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH,
> DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF,
> RCVD_IN_BARRACUDACENTRAL, SPF_HELO_PASS, SPF_PASS, TXREP,
> T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6
> From: Eli Zaretskii via Gdb-patches <gdb-patches@sourceware.org>
>
> > From: Tom Tromey <tromey@adacore.com>
> > Cc: Tom Tromey <tromey@adacore.com>, gdb-patches@sourceware.org
> > Date: Tue, 16 May 2023 13:06:09 -0600
> >
> > >> +@item env
> > >> +If provided, this should be an object where each value is a string.
> >
> > Eli> This doesn't say what should those string values be. Shouldn't they
> > Eli> be of the format "@var{variable}=@var{value}"?
> >
> > In JSON an object is a dictionary; so the keys are strings and the
> > values are as well. That is it looks like {"variable": "value"}
>
> That's okay, but I think we should say something about the structure
> of those strings, and {"variable": "value"} goes a long way towards
^^^^^^^
That should have been "objects", sorry.
> that.
@@ -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}.
@@ -13,20 +13,36 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+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")
new file mode 100644
@@ -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 <http://www.gnu.org/licenses/>. */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+int
+main (int argc, char *argv[])
+{
+ const char *value = getenv ("DEI");
+ const char *no_value = getenv ("NOSUCHVARIABLE");
+
+ return 0; /* BREAK */
+}
new file mode 100644
@@ -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 <http://www.gnu.org/licenses/>.
+
+# 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
@@ -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.