Skip to content

Commit acd5278

Browse files
committed
Add "cwd" parameter to DAP launch request
This adds the "cwd" parameter to the DAP launch request. This came up here: eclipse-cdt-cloud/cdt-gdb-adapter#90 ... and seemed like a good idea. Reviewed-By: Eli Zaretskii <[email protected]>
1 parent fc6485b commit acd5278

File tree

4 files changed

+70
-17
lines changed

4 files changed

+70
-17
lines changed

gdb/doc/gdb.texinfo

+8
Original file line numberDiff line numberDiff line change
@@ -39069,6 +39069,14 @@ If provided, this should be an array of strings. These strings are
3906939069
provided as command-line arguments to the inferior, as if by
3907039070
@code{set args}. @xref{Arguments}.
3907139071

39072+
@item cwd
39073+
If provided, this should be a string. @value{GDBN} will change its
39074+
working directory to this directory, as if by the @code{cd} command
39075+
(@pxref{Working Directory}). The launched program will inherit this
39076+
as its working directory. Note that change of directory happens
39077+
before the @code{program} parameter is processed. This will affect
39078+
the result if @code{program} is a relative filename.
39079+
3907239080
@item env
3907339081
If provided, this should be an object. Each key of the object will be
3907439082
used as the name of an environment variable; each value must be a

gdb/python/lib/gdb/dap/launch.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -23,47 +23,47 @@
2323
from .startup import send_gdb, send_gdb_with_response, in_gdb_thread, exec_and_log
2424

2525

26+
# The program being launched, or None. This should only be access
27+
# from the DAP thread.
2628
_program = None
2729

2830

2931
@in_gdb_thread
30-
def _set_args_env(args, env):
32+
def _launch_setup(program, cwd, args, env, stopAtBeginningOfMainSubprogram):
33+
if cwd is not None:
34+
exec_and_log("cd " + cwd)
35+
if program is not None:
36+
exec_and_log("file " + program)
3137
inf = gdb.selected_inferior()
38+
if stopAtBeginningOfMainSubprogram:
39+
main = inf.main_name
40+
if main is not None:
41+
exec_and_log("tbreak " + main)
3242
inf.arguments = args
3343
if env is not None:
3444
inf.clear_env()
3545
for name, value in env.items():
3646
inf.set_env(name, value)
3747

3848

39-
@in_gdb_thread
40-
def _break_at_main():
41-
inf = gdb.selected_inferior()
42-
main = inf.main_name
43-
if main is not None:
44-
exec_and_log("tbreak " + main)
45-
46-
4749
# Any parameters here are necessarily extensions -- DAP requires this
4850
# from implementations. Any additions or changes here should be
4951
# documented in the gdb manual.
5052
@request("launch")
5153
def launch(
5254
*,
5355
program: Optional[str] = None,
56+
cwd: Optional[str] = None,
5457
args: Sequence[str] = (),
5558
env: Optional[Mapping[str, str]] = None,
5659
stopAtBeginningOfMainSubprogram: bool = False,
5760
**extra,
5861
):
59-
if program is not None:
60-
global _program
61-
_program = program
62-
send_gdb("file " + _program)
63-
if stopAtBeginningOfMainSubprogram:
64-
send_gdb(_break_at_main)
65-
if len(args) > 0 or env is not None:
66-
send_gdb(lambda: _set_args_env(args, env))
62+
global _program
63+
_program = program
64+
send_gdb(
65+
lambda: _launch_setup(program, cwd, args, env, stopAtBeginningOfMainSubprogram)
66+
)
6767

6868

6969
@request("attach")

gdb/testsuite/gdb.dap/cwd.exp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2023 Free Software Foundation, Inc.
2+
3+
# This program is free software; you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License as published by
5+
# the Free Software Foundation; either version 3 of the License, or
6+
# (at your option) any later version.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
# Test the cwd extension.
17+
18+
require allow_dap_tests {!is_remote host}
19+
20+
load_lib dap-support.exp
21+
22+
standard_testfile attach.c
23+
24+
if {[build_executable ${testfile}.exp $testfile $srcfile] == -1} {
25+
return
26+
}
27+
28+
# Starting the inferior will fail if the change of cwd does not work.
29+
set the_dir [file dirname $testfile]
30+
set the_file [file tail $testfile]
31+
if {[dap_launch $the_file cwd $the_dir stop_at_main 1] == ""} {
32+
return
33+
}
34+
35+
dap_check_request_and_response "start inferior" configurationDone
36+
# We didn't explicitly set a breakpoint, so if we hit one, it worked.
37+
dap_wait_for_event_and_check "stopped at function breakpoint" stopped \
38+
"body reason" breakpoint
39+
40+
dap_shutdown

gdb/testsuite/lib/dap-support.exp

+5
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ proc _dap_initialize {name} {
248248
# * stop_at_main - value is ignored, the presence of this means that
249249
# "stopAtBeginningOfMainSubprogram" will be passed to the launch
250250
# request.
251+
# * cwd - value is the working directory to use.
251252
#
252253
# After this proc is called, gdb will be ready to accept breakpoint
253254
# requests.
@@ -284,6 +285,10 @@ proc dap_launch {file {args {}}} {
284285
append params { stopAtBeginningOfMainSubprogram [l true]}
285286
}
286287

288+
cwd {
289+
append envlist " cwd [format {[%s]} [list s $value]]"
290+
}
291+
287292
default {
288293
error "unrecognized parameter $key"
289294
}

0 commit comments

Comments
 (0)