From patchwork Wed May 17 15:52:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 69527 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 303A938555B4 for ; Wed, 17 May 2023 15:53:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 303A938555B4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684338838; bh=S2sr4mJRbFT7twZmHEtL7oOh/XmfdGBa8we51UjI9Kg=; h=Date:Subject:References:In-Reply-To:To:Cc:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=LRrt2B0CJg03JZJlaFl+nem0n6Bd8zL8grMQjw8k0KtN18P5jIQL3LZeiEP6D4lks tWgVP7a4I1L6ixbc4rx1F4rtRXb67ZNAF72E11z3HXWPCmDWwelQbO7jQty39rP+8u 0CgwWTFOYgJL6mGAZV1KwYdlYjrDsEjT3MQbyU1c= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd2e.google.com (mail-io1-xd2e.google.com [IPv6:2607:f8b0:4864:20::d2e]) by sourceware.org (Postfix) with ESMTPS id E9A213858C41 for ; Wed, 17 May 2023 15:52:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E9A213858C41 Received: by mail-io1-xd2e.google.com with SMTP id ca18e2360f4ac-76c6b32ee73so72760939f.1 for ; Wed, 17 May 2023 08:52:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684338756; x=1686930756; h=cc: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=S2sr4mJRbFT7twZmHEtL7oOh/XmfdGBa8we51UjI9Kg=; b=D9ITUIjxv11FsqlciIoKOSc27TGl1MOPCpt/zc37X6hb2EIXu9RZUwfucOu0LzEhV2 Y9KL/2gUTy8z47zikFHZVQxiL4kMsLqTitBG1ZxG5XFLo47N48PjlNm2xqzoE+rHC43c gx2q+ftYu0aoZZGTDAaIshcnaEpbsIgZLJwidN0So8aw2U5YXECY1hd9XcOt9junnr4Q AXoaPZpbXQk7b+wf1YFky10PndBoTjiDh78UwlM8XNHvbSg+XDWox3lTvgc+UvG/PGeD Qr3LfEmxnQKo8dXs98HKFe6ZbhlOHUdUQ/v/dQHiuY+ZUpLMG78tHyIvE8SofbtbsTpu uNfQ== X-Gm-Message-State: AC+VfDxEWQN1YyPIb83X+BrCB33+zSRp6jCtFMXw0ok6zxRN11FNCWcL rnTtdR38oLhwaFszMWG1rIWc61gwVN1g4N8JuGvhLw== X-Google-Smtp-Source: ACHHUZ7uvsmJ5hMCjmx43wf6clSVSg659XoaPaDfgqXD9gvUhFbx4z9G10o9TicpK2Zs2vkyigHMvg== X-Received: by 2002:a5d:9816:0:b0:753:13ec:4ba with SMTP id a22-20020a5d9816000000b0075313ec04bamr4649774iol.4.1684338756079; Wed, 17 May 2023 08:52:36 -0700 (PDT) Received: from localhost.localdomain (75-166-157-55.hlrn.qwest.net. [75.166.157.55]) by smtp.gmail.com with ESMTPSA id bs11-20020a056638450b00b0040f9a41e06asm8490145jab.0.2023.05.17.08.52.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 May 2023 08:52:35 -0700 (PDT) Date: Wed, 17 May 2023 09:52:29 -0600 Subject: [PATCH v2 2/2] Add "args" and "env" parameters to DAP launch request MIME-Version: 1.0 Message-Id: <20230509-dap-args-v2-2-095bf5392c53@adacore.com> References: <20230509-dap-args-v2-0-095bf5392c53@adacore.com> In-Reply-To: <20230509-dap-args-v2-0-095bf5392c53@adacore.com> To: gdb-patches@sourceware.org Cc: Andrew Burgess , Eli Zaretskii X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-10.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 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: Andrew Burgess Reviewed-By: Eli Zaretskii --- 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 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 . +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.