Add 'program' to DAP 'attach' request

Message ID 20231212143357.1314799-1-tromey@adacore.com
State New
Headers
Series Add 'program' to DAP 'attach' request |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed

Commit Message

Tom Tromey Dec. 12, 2023, 2:33 p.m. UTC
  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.
---
 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(-)
  

Comments

Eli Zaretskii Dec. 12, 2023, 2:51 p.m. UTC | #1
> From: Tom Tromey <tromey@adacore.com>
> Cc: Tom Tromey <tromey@adacore.com>
> Date: Tue, 12 Dec 2023 07:33:57 -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.
> ---
>  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(-)

The documentation parts are OK, thanks.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
  
Tom Tromey Dec. 22, 2023, 4:05 p.m. UTC | #2
>>>>> "Tom" == Tom Tromey <tromey@adacore.com> writes:

Tom> In many cases, it's not possible for gdb to discover the executable
Tom> when a DAP 'attach' request is used.  This patch lets the IDE supply
Tom> this information.

I'm checking this in.

Tom
  

Patch

diff --git a/gdb/NEWS b/gdb/NEWS
index 534e2e7f364..3c17d09203d 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 6e4adf512ee..be79b34ad2c 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -39579,12 +39579,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 7014047ff51..675542c92d0 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 5b308f94975..4d562711f09 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 b9ac314fee5..8186148ee0a 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