From eb6476e2db406ba323efba438b0d1851e86d02e5 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Thu, 7 Dec 2023 09:51:52 -0700 Subject: [PATCH] Add 'program' to DAP 'attach' request In many cases, it's not possible for gdb to discover the executable when a DAP 'attach' request is used. This patch lets the IDE supply this information. Reviewed-By: Eli Zaretskii --- gdb/NEWS | 2 ++ gdb/doc/gdb.texinfo | 11 ++++++++++- gdb/python/lib/gdb/dap/launch.py | 25 +++++++++++++++++++++---- gdb/testsuite/gdb.dap/attach.exp | 2 +- gdb/testsuite/lib/dap-support.exp | 11 ++++++++--- 5 files changed, 42 insertions(+), 9 deletions(-) diff --git a/gdb/NEWS b/gdb/NEWS index 534e2e7..3c17d09 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -88,6 +88,8 @@ show remote thread-options-packet ** GDB now supports the "cancel" request. + ** The "attach" request now supports specifying the program. + * New remote packets New stop reason: clone diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 7a5d357..3cb7682 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -39603,12 +39603,21 @@ the same approach as the @code{start} command. @xref{Starting}. @end table @value{GDBN} defines some parameters that can be passed to the -@code{attach} request. One of these must be specified. +@code{attach} request. Either @code{pid} or @code{target} must be +specified, but if both are specified then @code{target} will be +ignored. @table @code @item pid The process ID to which @value{GDBN} should attach. @xref{Attach}. +@item program +If provided, this is a string that specifies the program to use. This +corresponds to the @code{file} command. @xref{Files}. In some cases, +@value{GDBN} can automatically determine which program is running. +However, for many remote targets, this is not the case, and so this +should be supplied. + @item target The target to which @value{GDBN} should connect. This is a string and is passed to the @code{target remote} command. @xref{Connecting}. diff --git a/gdb/python/lib/gdb/dap/launch.py b/gdb/python/lib/gdb/dap/launch.py index 7014047..675542c 100644 --- a/gdb/python/lib/gdb/dap/launch.py +++ b/gdb/python/lib/gdb/dap/launch.py @@ -28,6 +28,11 @@ from .startup import exec_and_log _program = None +# True if the program was attached, False otherwise. This should only +# be accessed from the gdb thread. +_attach = False + + # Any parameters here are necessarily extensions -- DAP requires this # from implementations. Any additions or changes here should be # documented in the gdb manual. @@ -43,6 +48,8 @@ def launch( ): global _program _program = program + global _attach + _attach = False if cwd is not None: exec_and_log("cd " + cwd) if program is not None: @@ -60,10 +67,20 @@ def launch( @request("attach") -def attach(*, pid: Optional[int] = None, target: Optional[str] = None, **args): +def attach( + *, + program: Optional[str] = None, + pid: Optional[int] = None, + target: Optional[str] = None, + **args, +): # Ensure configurationDone does not try to run. + global _attach + _attach = True global _program - _program = None + _program = program + if program is not None: + exec_and_log("file " + program) if pid is not None: cmd = "attach " + str(pid) elif target is not None: @@ -77,7 +94,7 @@ def attach(*, pid: Optional[int] = None, target: Optional[str] = None, **args): @capability("supportsConfigurationDoneRequest") @request("configurationDone", response=False) def config_done(**args): - global _program - if _program is not None: + global _attach + if not _attach: expect_process("process") exec_and_expect_stop("run") diff --git a/gdb/testsuite/gdb.dap/attach.exp b/gdb/testsuite/gdb.dap/attach.exp index 5b308f9..4d56271 100644 --- a/gdb/testsuite/gdb.dap/attach.exp +++ b/gdb/testsuite/gdb.dap/attach.exp @@ -29,7 +29,7 @@ set test_spawn_id [spawn_wait_for_attach $binfile] set testpid [spawn_id_get_pid $test_spawn_id] # We just want to test that attaching works at all. -if {[dap_attach $testpid] != ""} { +if {[dap_attach $testpid $binfile] != ""} { dap_shutdown true } diff --git a/gdb/testsuite/lib/dap-support.exp b/gdb/testsuite/lib/dap-support.exp index b9ac314..8186148 100644 --- a/gdb/testsuite/lib/dap-support.exp +++ b/gdb/testsuite/lib/dap-support.exp @@ -306,12 +306,17 @@ proc dap_launch {file {args {}}} { # Start gdb, send a DAP initialize request, and then an attach request # specifying PID as the inferior process ID. Returns the empty string # on failure, or the response object from the attach request. -proc dap_attach {pid} { +proc dap_attach {pid {prog ""}} { if {[_dap_initialize "startup - initialize"] == ""} { return "" } - return [dap_check_request_and_response "startup - attach" attach \ - [format {o pid [i %s]} $pid]] + + set args [format {o pid [i %s]} $pid] + if {$prog != ""} { + append args [format { program [s %s]} $prog] + } + + return [dap_check_request_and_response "startup - attach" attach $args] } # Start gdb, send a DAP initialize request, and then an attach request -- 2.7.4