[v2,2/2] Add "args" and "env" parameters to DAP launch request

Message ID 20230509-dap-args-v2-2-095bf5392c53@adacore.com
State New
Headers
Series Add more DAP launch parameters |

Commit Message

Tom Tromey May 17, 2023, 3:52 p.m. UTC
  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: Andrew Burgess <aburgess@redhat.com>
Reviewed-By: Eli Zaretskii <eliz@gnu.org>
---
 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(-)
  

Patch

diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 531147f6e6b..9570f12fa92 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.  Each key of the object will be
+used as the name of an environment variable; each value must be a
+string and will be the value of that variable.  The environment of the
+inferior will be set to exactly as passed in.  @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.