From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-io1-xd2a.google.com (mail-io1-xd2a.google.com [IPv6:2607:f8b0:4864:20::d2a]) by sourceware.org (Postfix) with ESMTPS id 71D5C3857711 for ; Tue, 25 Jul 2023 13:49:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 71D5C3857711 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-io1-xd2a.google.com with SMTP id ca18e2360f4ac-783698a37beso307878339f.0 for ; Tue, 25 Jul 2023 06:49:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1690292981; x=1690897781; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=fS5n8VqkjRUcioSRuJPHQYEAb+OL0L2iC1QKlJUeLyk=; b=GvtjKKdT77IiZxYX4rA3bwK9oECiFVSypsepaBCbkPBFfLY1VnsGJyQBdFqoXB2tKt rTL40kgV+UGrZU7E+naK3uVHuD5MouIdRX93oyS3ya7hnZEWy3jv+f4fykb+oKBi15UT yeseA3hMMsWE3dErCM95puX+UR+Y4cSK3u4Z1wNmswEOVsmlq0oP8tvxqj5W4sDpNtYX sa/LPQ8mALzk7ss6kukbYsr3svish2sCqfCh1y5R56RLiSPvPg3/V3PbvE805akedCCH ORVX7Sa6Z8aZEsIINQ+0W9B3PkH46o+tKdxEewTzt2itOMRW0qR6SFCypmiPHqOuR1vb Jmiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690292981; x=1690897781; 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=fS5n8VqkjRUcioSRuJPHQYEAb+OL0L2iC1QKlJUeLyk=; b=Fo6Lim9hZ6lST/ItbWhl6QhrTuLM7kJMI8wj4LJWZ+j7jCHJmcU6srWX78ndiqWtu3 ktE+v2gCVIpf68TdK+rhVhTWweiR3N7Sqh6iFHhuKmD3+I1BoeIWFSHii7VYiGU15mHc 05KJK6G5Irm/L21qaV0M+Uyo5nbLhuzKPJTkNSC3OnzYBm8KsP2ucrSgV1Rxfzlnu5y7 JONeCEudtArPsiSd+HeP773/E0mFjFvpn5jWuIGAhYV36mvaFsou6Aa9eOE7sfsAG8Kr GUmrBa6E4W/3pU23CbrYyjTBD1LxiLThDD/cNXZ91KmT7Cu1XFub4d0/8AYFTO37igas OWDQ== X-Gm-Message-State: ABy/qLYBMmzDjHGip07nMTj/XGgL1FS8VxQCG5oMfzRVO32KVJW7G/hK VTJiaoKLplzruf+phCVugotsVP/87cZbchbTuUB4Gw== X-Google-Smtp-Source: APBJJlGm2WW0rJE+nFgvE6r6awOt2624XBipwOP9IaNc38Bds8ZD/YPOZwKRzbFetb1DlFCLCnx3HQ== X-Received: by 2002:a5d:870b:0:b0:787:4b5f:b5ef with SMTP id u11-20020a5d870b000000b007874b5fb5efmr2933287iom.4.1690292981592; Tue, 25 Jul 2023 06:49:41 -0700 (PDT) Received: from localhost.localdomain (75-166-135-140.hlrn.qwest.net. [75.166.135.140]) by smtp.gmail.com with ESMTPSA id c3-20020a02a403000000b00418a5e0e93esm3595018jal.162.2023.07.25.06.49.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Jul 2023 06:49:41 -0700 (PDT) From: Tom Tromey Date: Tue, 25 Jul 2023 07:49:40 -0600 Subject: [PATCH 3/6] Add "cwd" parameter to DAP launch request MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230725-dap-bt-path-v1-3-bb015b0d8e54@adacore.com> References: <20230725-dap-bt-path-v1-0-bb015b0d8e54@adacore.com> In-Reply-To: <20230725-dap-bt-path-v1-0-bb015b0d8e54@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.3 X-Spam-Status: No, score=-11.5 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 List-Id: This adds the "cwd" parameter to the DAP launch request. This came up here: https://github.com/eclipse-cdt-cloud/cdt-gdb-adapter/issues/90 ... and seemed like a good idea. --- gdb/doc/gdb.texinfo | 8 ++++++++ gdb/python/lib/gdb/dap/launch.py | 34 ++++++++++++++++----------------- gdb/testsuite/gdb.dap/cwd.exp | 40 +++++++++++++++++++++++++++++++++++++++ gdb/testsuite/lib/dap-support.exp | 5 +++++ 4 files changed, 70 insertions(+), 17 deletions(-) diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 1463f501768..bd11f0a4475 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -39069,6 +39069,14 @@ 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 cwd +If provided, this should be a string. @value{GDBN} will change its +working directory to this directory, as if by the @code{cd} command +(@pxref{Working Directory}). The launched program will inherit this +as its working directory. Note that change of directory happens +before the @code{program} parameter is processed. This will affect +the result if @code{program} is a relative filename. + @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 diff --git a/gdb/python/lib/gdb/dap/launch.py b/gdb/python/lib/gdb/dap/launch.py index c3c09bc3dd0..d13037fa476 100644 --- a/gdb/python/lib/gdb/dap/launch.py +++ b/gdb/python/lib/gdb/dap/launch.py @@ -23,12 +23,22 @@ from .server import request, capability from .startup import send_gdb, send_gdb_with_response, in_gdb_thread, exec_and_log +# The program being launched, or None. This should only be access +# from the DAP thread. _program = None @in_gdb_thread -def _set_args_env(args, env): +def _launch_setup(program, cwd, args, env, stopAtBeginningOfMainSubprogram): + if cwd is not None: + exec_and_log("cd " + cwd) + if program is not None: + exec_and_log("file " + program) inf = gdb.selected_inferior() + if stopAtBeginningOfMainSubprogram: + main = inf.main_name + if main is not None: + exec_and_log("tbreak " + main) inf.arguments = args if env is not None: inf.clear_env() @@ -36,14 +46,6 @@ def _set_args_env(args, env): inf.set_env(name, value) -@in_gdb_thread -def _break_at_main(): - inf = gdb.selected_inferior() - main = inf.main_name - if main is not None: - exec_and_log("tbreak " + main) - - # Any parameters here are necessarily extensions -- DAP requires this # from implementations. Any additions or changes here should be # documented in the gdb manual. @@ -51,19 +53,17 @@ def _break_at_main(): def launch( *, program: Optional[str] = None, + cwd: Optional[str] = None, args: Sequence[str] = (), env: Optional[Mapping[str, str]] = None, stopAtBeginningOfMainSubprogram: bool = False, **extra, ): - if program is not None: - global _program - _program = program - send_gdb("file " + _program) - if stopAtBeginningOfMainSubprogram: - send_gdb(_break_at_main) - if len(args) > 0 or env is not None: - send_gdb(lambda: _set_args_env(args, env)) + global _program + _program = program + send_gdb( + lambda: _launch_setup(program, cwd, args, env, stopAtBeginningOfMainSubprogram) + ) @request("attach") diff --git a/gdb/testsuite/gdb.dap/cwd.exp b/gdb/testsuite/gdb.dap/cwd.exp new file mode 100644 index 00000000000..394fe5ada83 --- /dev/null +++ b/gdb/testsuite/gdb.dap/cwd.exp @@ -0,0 +1,40 @@ +# 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 the cwd extension. + +require allow_dap_tests {!is_remote host} + +load_lib dap-support.exp + +standard_testfile attach.c + +if {[build_executable ${testfile}.exp $testfile $srcfile] == -1} { + return +} + +# Starting the inferior will fail if the change of cwd does not work. +set the_dir [file dirname $testfile] +set the_file [file tail $testfile] +if {[dap_launch $the_file cwd $the_dir stop_at_main 1] == ""} { + return +} + +dap_check_request_and_response "start inferior" configurationDone +# We didn't explicitly set a breakpoint, so if we hit one, it worked. +dap_wait_for_event_and_check "stopped at function breakpoint" stopped \ + "body reason" breakpoint + +dap_shutdown diff --git a/gdb/testsuite/lib/dap-support.exp b/gdb/testsuite/lib/dap-support.exp index 4a1a288e7ae..657ad7b29bc 100644 --- a/gdb/testsuite/lib/dap-support.exp +++ b/gdb/testsuite/lib/dap-support.exp @@ -248,6 +248,7 @@ proc _dap_initialize {name} { # * stop_at_main - value is ignored, the presence of this means that # "stopAtBeginningOfMainSubprogram" will be passed to the launch # request. +# * cwd - value is the working directory to use. # # After this proc is called, gdb will be ready to accept breakpoint # requests. @@ -284,6 +285,10 @@ proc dap_launch {file {args {}}} { append params { stopAtBeginningOfMainSubprogram [l true]} } + cwd { + append envlist " cwd [format {[%s]} [list s $value]]" + } + default { error "unrecognized parameter $key" } -- 2.40.1