RFC: ``detach remote''

Daniel Jacobowitz
2002-08-06T21:00:08Z

[-- Attachment #1: Type: text/plain, Size: 822 bytes --]

So, the GDB manual has this to say about remote debugging:

To resume the remote program and stop debugging it, use the `detach'
command.

This is not how gdbserver works, and it isn't how any stubs I've worked with
behave, either. They'll sit and wait for a reconnection. Rather than
change the status quo, I would like to update the documentation and provide
a way to get the previously documented behavior. This uses the "K" packet
described in my email to gdb@ last week. The new command is `detach remote'
which I'm not thrilled with but I'm a little short on ideas. It's hard to
find something to call this.

GDB and gdbserver patches attached. Comments?

--
Daniel Jacobowitz Carnegie Mellon University
MontaVista Software Debian GNU/Linux Developer

[-- Attachment #2: detach-2.client.patch --]
[-- Type: text/plain, Size: 4364 bytes --]

2002-08-06 Daniel Jacobowitz <drow@mvista.com>

* remote.c (remote_detach): Support `detach remote'.
(remote_async_detach): Likewise.

2002-08-06 Daniel Jacobowitz <drow@mvista.com>

* gdb.texinfo: Document `detach remote'.
(Remote Protocol): Document `K' packet.

Index: remote.c
===================================================================
RCS file: /cvs/src/src/gdb/remote.c,v
retrieving revision 1.89
diff -u -p -r1.89 remote.c
--- remote.c 23 Jul 2002 18:55:06 -0000 1.89
+++ remote.c 6 Aug 2002 20:49:59 -0000
@@ -2496,17 +2496,28 @@ remote_detach (char *args, int from_tty)
struct remote_state *rs = get_remote_state ();
char *buf = alloca (rs->remote_packet_size);

- if (args)
- error ("Argument given to \"detach\" when remotely debugging.");
-
- /* Tell the remote target to detach. */
- strcpy (buf, "D");
- remote_send (buf, (rs->remote_packet_size));
+ if (args && strcmp (args, "remote") == 0)
+ {
+ /* Tell the target to detach and resume the inferior. */
+ putpkt ("K");
+ getpkt (buf, rs->remote_packet_size, 0);
+ if (strcmp (buf, "ERR") == 0)
+ error ("Remote target refused to detach.");
+ else if (strcmp (buf, "OK") != 0)
+ error ("Remote target does not support detaching.");
+ }
+ else if (args)
+ error ("Unknown argument given to \"detach\".");
+ else
+ {
+ /* Tell the remote target to detach. */
+ strcpy (buf, "D");
+ remote_send (buf, (rs->remote_packet_size));
+ }

target_mourn_inferior ();
if (from_tty)
puts_filtered ("Ending remote debugging.\n");
-
}

/* Same as remote_detach, but with async support. */
@@ -2516,12 +2527,24 @@ remote_async_detach (char *args, int fro
struct remote_state *rs = get_remote_state ();
char *buf = alloca (rs->remote_packet_size);

- if (args)
- error ("Argument given to \"detach\" when remotely debugging.");
-
- /* Tell the remote target to detach. */
- strcpy (buf, "D");
- remote_send (buf, (rs->remote_packet_size));
+ if (args && strcmp (args, "remote") == 0)
+ {
+ /* Tell the target to detach and resume the inferior. */
+ putpkt ("K");
+ getpkt (buf, rs->remote_packet_size, 0);
+ if (strcmp (buf, "ERR") == 0)
+ error ("Remote target refused to detach.");
+ else if (strcmp (buf, "OK") != 0)
+ error ("Remote target does not support detaching.");
+ }
+ else if (args)
+ error ("Unknown argument given to \"detach\".");
+ else
+ {
+ /* Tell the remote target to detach. */
+ strcpy (buf, "D");
+ remote_send (buf, (rs->remote_packet_size));
+ }

/* Unregister the file descriptor from the event loop. */
if (target_is_async_p ())
Index: doc/gdb.texinfo
===================================================================
RCS file: /cvs/src/src/gdb/doc/gdb.texinfo,v
retrieving revision 1.107
diff -u -p -r1.107 gdb.texinfo
--- doc/gdb.texinfo 3 Aug 2002 23:37:07 -0000 1.107
+++ doc/gdb.texinfo 6 Aug 2002 20:50:03 -0000
@@ -10784,8 +10784,11 @@ session.
Now you can use all the usual commands to examine and change data and to
step and continue the remote program.

-To resume the remote program and stop debugging it, use the @code{detach}
-command.
+To end remote debugging, use the @code{detach} command. Most remote stubs
+will wait for @value{GDBN} to reconnect; for some stubs, you can use
+@code{detach remote} to cause the stub to resume the remote program.
+Depending on the remote stub, this may cause the stub to exit or become
+unavailable.

@cindex interrupting remote programs
@cindex remote programs, interrupting
@@ -14502,6 +14505,20 @@ See @samp{i} and @samp{S} for likely syn
@tab
FIXME: @emph{There is no description of how to operate when a specific
thread context has been selected (i.e.@: does 'k' kill only that thread?)}.
+
+@item Detach request
+@tab @code{K}
+@tab
+Detach @value{GDBN} from the remote system. Sent to the remote target before
+@value{GDBN} disconnects. This packet indicates that the inferior should
+be resumed instead of kept stopped, waiting for @value{GDBN} to reconnect.
+The stub may exit after replying to this packet, or may wait for @value{GDBN}.
+@item
+@tab reply @code{ERR}
+@tab The stub should return failure if it can not detach or refuses to detach.
+@item
+@tab reply @code{OK}
+@tab Success

@item reserved
@tab @code{l}

[-- Attachment #3: detach-2.server.patch --]
[-- Type: text/plain, Size: 3062 bytes --]

2002-08-06 Daniel Jacobowitz <drow@mvista.com>

* gdbserver/linux-low.c (linux_detach_one_process)
(linux_detach): New.
(linux_target_ops): Add linux_detach.
* gdbserver/server.c (main): Recognize `K' packet.
* gdbserver/target.h (struct target_ops): Add ``detach''
member.
(detach_inferior): New.

Index: gdbserver/linux-low.c
===================================================================
RCS file: /cvs/src/src/gdb/gdbserver/linux-low.c,v
retrieving revision 1.18
diff -u -p -r1.18 linux-low.c
--- gdbserver/linux-low.c 18 Jul 2002 15:18:02 -0000 1.18
+++ gdbserver/linux-low.c 6 Aug 2002 20:50:03 -0000
@@ -232,13 +232,28 @@ linux_kill_one_process (struct inferior_
} while (WIFSTOPPED (wstat));
}

-/* Return nonzero if the given thread is still alive. */
static void
linux_kill (void)
{
for_each_inferior (&all_threads, linux_kill_one_process);
}

+static void
+linux_detach_one_process (struct inferior_list_entry *entry)
+{
+ struct thread_info *thread = (struct thread_info *) entry;
+ struct process_info *process = get_thread_process (thread);
+
+ ptrace (PTRACE_DETACH, pid_of (process), 0, 0);
+}
+
+static void
+linux_detach (void)
+{
+ for_each_inferior (&all_threads, linux_detach_one_process);
+}
+
+/* Return nonzero if the given thread is still alive. */
static int
linux_thread_alive (int tid)
{
@@ -1261,6 +1276,7 @@ static struct target_ops linux_target_op
linux_create_inferior,
linux_attach,
linux_kill,
+ linux_detach,
linux_thread_alive,
linux_resume,
linux_wait,
Index: gdbserver/server.c
===================================================================
RCS file: /cvs/src/src/gdb/gdbserver/server.c,v
retrieving revision 1.13
diff -u -p -r1.13 server.c
--- gdbserver/server.c 11 Jun 2002 17:32:40 -0000 1.13
+++ gdbserver/server.c 6 Aug 2002 20:50:03 -0000
@@ -310,6 +310,19 @@ main (int argc, char *argv[])
exit (0);
break;
}
+ case 'K':
+ if (attached)
+ {
+ fprintf (stderr, "Detaching from inferior\n");
+ detach_inferior ();
+ write_ok (own_buf);
+ putpkt (own_buf);
+ remote_close ();
+ exit (0);
+ }
+ else
+ write_enn (own_buf);
+ break;
case 'T':
if (mythread_alive (strtol (&own_buf[1], NULL, 16)))
write_ok (own_buf);
Index: gdbserver/target.h
===================================================================
RCS file: /cvs/src/src/gdb/gdbserver/target.h,v
retrieving revision 1.4
diff -u -p -r1.4 target.h
--- gdbserver/target.h 11 Jun 2002 17:32:40 -0000 1.4
+++ gdbserver/target.h 6 Aug 2002 20:50:03 -0000
@@ -48,6 +48,10 @@ struct target_ops

void (*kill) (void);

+ /* Detach from all inferiors. */
+
+ void (*detach) (void);
+
/* Return 1 iff the thread with process ID PID is alive. */

int (*thread_alive) (int pid);
@@ -123,6 +127,9 @@ void set_target_ops (struct target_ops *

#define kill_inferior() \
(*the_target->kill) ()
+
+#define detach_inferior() \
+ (*the_target->detach) ()

#define mythread_alive(pid) \
(*the_target->thread_alive) (pid)