]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Add 'program' to DAP 'attach' request
authorTom Tromey <tromey@adacore.com>
Thu, 7 Dec 2023 16:51:52 +0000 (09:51 -0700)
committerTom Tromey <tromey@adacore.com>
Fri, 22 Dec 2023 16:05:17 +0000 (09:05 -0700)
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 <eliz@gnu.org>
gdb/NEWS
gdb/doc/gdb.texinfo
gdb/python/lib/gdb/dap/launch.py
gdb/testsuite/gdb.dap/attach.exp
gdb/testsuite/lib/dap-support.exp

index 534e2e7f36466942557ce2d5ca3460d45bddf17b..3c17d09203d8a6711cf2a7e6d76c0928454520b6 100644 (file)
--- 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
index 7a5d357b99bbac411ba2a99277894a8ceb373189..3cb7682cdb517f3f51c6da62e96c0a31bdfde53b 100644 (file)
@@ -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}.
index 7014047ff51c298e4bdddab880d043c2d7748cf4..675542c92d04400ae10f18cf44d6395a66bdb280 100644 (file)
@@ -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")
index 5b308f9497542cb963f18178fb38d3db12003bde..4d562711f09ed374983f250da1d791aad77c2fc0 100644 (file)
@@ -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
 }
 
index b9ac314fee54b4047d54515f069f361f6a0b3b85..8186148ee0aea936231d4b7e05abfb18617fb1ad 100644 (file)
@@ -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