Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [RFA] gdbserver --attach support
@ 2002-01-17 12:25 Daniel Jacobowitz
  2002-01-17 12:31 ` Andrew Cagney
  2002-01-18  0:17 ` Eli Zaretskii
  0 siblings, 2 replies; 9+ messages in thread
From: Daniel Jacobowitz @ 2002-01-17 12:25 UTC (permalink / raw)
  To: gdb-patches

Someone else did this some time ago, but got lost in the paper trail, I
think.  This adds '--attach <pid>' as an alternative for 'prog [args...]'. 
I only implemented it for Linux, since it's the only one I know the
appropriate ptrace command for with any certainty; other architectures will
still link but report it as unsupported if you try to use --attach.

Is this OK to commit?  (Andrew, I guess this one's yours - the only person
who had been watching gdbserver particularly was J.T.).

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

2002-01-17  Daniel Jacobowitz  <drow@mvista.com>

	* low-hppabsd.c (myattach): New function, returning -1.
	* low-lynx.c (myattach): Likewise.
	* low-nbsd.c (myattach): Likewise.
	* low-sim.c (myattach): Likewise.
	* low-sparc.c (myattach): Likewise.
	* low-sun3.c (myattach): Likewise.

	* low-linux.c (myattach): New function.

	* server.c (attach_inferior): New function.
	(main): Handle "--attach".

Index: gdb/gdbserver/low-hppabsd.c
===================================================================
RCS file: /cvs/src/src/gdb/gdbserver/low-hppabsd.c,v
retrieving revision 1.9
diff -u -p -r1.9 low-hppabsd.c
--- low-hppabsd.c	2001/07/26 02:23:57	1.9
+++ low-hppabsd.c	2002/01/17 19:31:29
@@ -81,6 +81,13 @@ kill_inferior (void)
 /*************inferior_died ();****VK**************/
 }
 
+/* Attaching is not supported.  */
+int
+myattach (int pid)
+{
+  return -1;
+}
+
 /* Return nonzero if the given thread is still alive.  */
 int
 mythread_alive (int pid)
Index: gdb/gdbserver/low-linux.c
===================================================================
RCS file: /cvs/src/src/gdb/gdbserver/low-linux.c,v
retrieving revision 1.12
diff -u -p -r1.12 low-linux.c
--- low-linux.c	2001/12/05 15:11:48	1.12
+++ low-linux.c	2002/01/17 19:31:29
@@ -78,6 +78,23 @@ create_inferior (char *program, char **a
   return pid;
 }
 
+/* Attach to an inferior process.  */
+
+int
+myattach (int pid)
+{
+  if (ptrace (PTRACE_ATTACH, pid, 0, 0) != 0)
+    {
+      fprintf (stderr, "Cannot attach to process %d: %s (%d)\n", pid,
+	       errno < sys_nerr ? sys_errlist[errno] : "unknown error",
+	       errno);
+      fflush (stderr);
+      _exit (0177);
+    }
+
+  return 0;
+}
+
 /* Kill the inferior process.  Make us have no inferior.  */
 
 void
Index: gdb/gdbserver/low-lynx.c
===================================================================
RCS file: /cvs/src/src/gdb/gdbserver/low-lynx.c,v
retrieving revision 1.4
diff -u -p -r1.4 low-lynx.c
--- low-lynx.c	2001/07/26 02:23:58	1.4
+++ low-lynx.c	2002/01/17 19:31:29
@@ -87,6 +87,13 @@ create_inferior (char *program, char **a
   return pid;
 }
 
+/* Attaching is not supported.  */
+int
+myattach (int pid)
+{
+  return -1;
+}
+
 /* Kill the inferior process.  Make us have no inferior.  */
 
 void
Index: gdb/gdbserver/low-nbsd.c
===================================================================
RCS file: /cvs/src/src/gdb/gdbserver/low-nbsd.c,v
retrieving revision 1.11
diff -u -p -r1.11 low-nbsd.c
--- low-nbsd.c	2001/07/26 02:23:58	1.11
+++ low-nbsd.c	2002/01/17 19:31:29
@@ -145,6 +145,13 @@ create_inferior (char *program, char **a
   return pid;
 }
 
+/* Attaching is not supported.  */
+int
+myattach (int pid)
+{
+  return -1;
+}
+
 /* Kill the inferior process.  Make us have no inferior.  */
 
 void
Index: gdb/gdbserver/low-sim.c
===================================================================
RCS file: /cvs/src/src/gdb/gdbserver/low-sim.c,v
retrieving revision 1.4
diff -u -p -r1.4 low-sim.c
--- low-sim.c	2001/03/06 08:21:44	1.4
+++ low-sim.c	2002/01/17 19:31:29
@@ -139,6 +139,13 @@ create_inferior (char *program, char **a
   return pid;
 }
 
+/* Attaching is not supported.  */
+int
+myattach (int pid)
+{
+  return -1;
+}
+
 /* Kill the inferior process.  Make us have no inferior.  */
 
 void
Index: gdb/gdbserver/low-sparc.c
===================================================================
RCS file: /cvs/src/src/gdb/gdbserver/low-sparc.c,v
retrieving revision 1.8
diff -u -p -r1.8 low-sparc.c
--- low-sparc.c	2001/07/26 02:23:58	1.8
+++ low-sparc.c	2002/01/17 19:31:29
@@ -75,6 +75,13 @@ create_inferior (char *program, char **a
   return pid;
 }
 
+/* Attaching is not supported.  */
+int
+myattach (int pid)
+{
+  return -1;
+}
+
 /* Kill the inferior process.  Make us have no inferior.  */
 
 void
Index: gdb/gdbserver/low-sun3.c
===================================================================
RCS file: /cvs/src/src/gdb/gdbserver/low-sun3.c,v
retrieving revision 1.7
diff -u -p -r1.7 low-sun3.c
--- low-sun3.c	2001/07/26 02:23:58	1.7
+++ low-sun3.c	2002/01/17 19:31:29
@@ -72,6 +72,13 @@ create_inferior (char *program, char **a
   return pid;
 }
 
+/* Attaching is not supported.  */
+int
+myattach (int pid)
+{
+  return -1;
+}
+
 /* Kill the inferior process.  Make us have no inferior.  */
 
 void
Index: gdb/gdbserver/server.c
===================================================================
RCS file: /cvs/src/src/gdb/gdbserver/server.c,v
retrieving revision 1.3
diff -u -p -r1.3 server.c
--- server.c	2001/03/06 08:21:44	1.3
+++ server.c	2002/01/17 19:31:29
@@ -39,6 +39,21 @@ start_inferior (char *argv[], char *stat
   return mywait (statusptr);
 }
 
+static int
+attach_inferior (int pid, char *statusptr, unsigned char *sigptr)
+{
+  /* myattach should return -1 if attaching is unsupported,
+     0 if it succeeded, and call error() otherwise.  */
+  if (myattach (pid) != 0)
+    return -1;
+
+  inferior_pid = pid;
+
+  *sigptr = mywait (statusptr);
+
+  return 0;
+}
+
 extern int remote_debug;
 
 int
@@ -49,6 +64,10 @@ main (int argc, char *argv[])
   unsigned char signal;
   unsigned int len;
   CORE_ADDR mem_addr;
+  int bad_attach = 0;
+  int pid = 0;
+  int attached = 0;
+  char *arg_end;
 
   if (setjmp (toplevel))
     {
@@ -56,15 +75,44 @@ main (int argc, char *argv[])
       exit (1);
     }
 
-  if (argc < 3)
-    error ("Usage: gdbserver tty prog [args ...]");
+  if (argc >= 3 && strcmp (argv[2], "--attach") == 0)
+    {
+      if (argc == 4
+	  && argv[3] != '\0'
+	  && (pid = strtoul (argv[3], &arg_end, 10)) != 0
+	  && *arg_end == '\0')
+	{
+	  ;
+	}
+      else
+	bad_attach = 1;
+    }
 
+  if (argc < 3 || bad_attach)
+    error ("Usage:\tgdbserver tty prog [args ...]\n"
+		 "\tgdbserver tty --attach pid");
+
   initialize_low ();
 
-  /* Wait till we are at first instruction in program.  */
-  signal = start_inferior (&argv[2], &status);
+  if (pid == 0)
+    {
+      /* Wait till we are at first instruction in program.  */
+      signal = start_inferior (&argv[2], &status);
 
-  /* We are now stopped at the first instruction of the target process */
+      /* We are now stopped at the first instruction of the target process */
+    }
+  else
+    {
+      switch (attach_inferior (pid, &status, &signal))
+	{
+	case -1:
+	  error ("Attaching not supported on this target");
+	  break;
+	default:
+	  attached = 1;
+	  break;
+	}
+    }
 
   while (1)
     {
@@ -83,8 +131,17 @@ main (int argc, char *argv[])
 	      remote_debug = !remote_debug;
 	      break;
 	    case '!':
-	      extended_protocol = 1;
-	      prepare_resume_reply (own_buf, status, signal);
+	      if (attached == 0)
+		{
+		  extended_protocol = 1;
+		  prepare_resume_reply (own_buf, status, signal);
+		}
+	      else
+		{
+		  /* We can not use the extended protocol if we are
+		     attached.  */
+		  own_buf[0] = '\0';
+		}
 	      break;
 	    case '?':
 	      prepare_resume_reply (own_buf, status, signal);
@@ -250,8 +307,8 @@ main (int argc, char *argv[])
 	}
       else
 	{
-	  fprintf (stderr, "Remote side has terminated connection.  GDBserver will reopen the connection.\n");
-
+	  fprintf (stderr, "Remote side has terminated connection.  "
+			   "GDBserver will reopen the connection.\n");
 	  remote_close ();
 	}
     }


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [RFA] gdbserver --attach support
  2002-01-17 12:25 [RFA] gdbserver --attach support Daniel Jacobowitz
@ 2002-01-17 12:31 ` Andrew Cagney
  2002-01-17 12:46   ` Daniel Jacobowitz
  2002-01-18  0:17 ` Eli Zaretskii
  1 sibling, 1 reply; 9+ messages in thread
From: Andrew Cagney @ 2002-01-17 12:31 UTC (permalink / raw)
  To: Daniel Jacobowitz; +Cc: gdb-patches

Yes, ok.


> +		  /* We can not use the extended protocol if we are
> +		     attached.  */


can you just add a little bit more to this comment - in particular 
restart the program if we are attached.

Andrew





^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [RFA] gdbserver --attach support
  2002-01-17 12:31 ` Andrew Cagney
@ 2002-01-17 12:46   ` Daniel Jacobowitz
  0 siblings, 0 replies; 9+ messages in thread
From: Daniel Jacobowitz @ 2002-01-17 12:46 UTC (permalink / raw)
  To: gdb-patches

On Thu, Jan 17, 2002 at 03:31:36PM -0500, Andrew Cagney wrote:
> Yes, ok.
> 
> 
> >+		  /* We can not use the extended protocol if we are
> >+		     attached.  */
> 
> 
> can you just add a little bit more to this comment - in particular 
> restart the program if we are attached.

Committed, with a better comment.  Thanks.

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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [RFA] gdbserver --attach support
  2002-01-17 12:25 [RFA] gdbserver --attach support Daniel Jacobowitz
  2002-01-17 12:31 ` Andrew Cagney
@ 2002-01-18  0:17 ` Eli Zaretskii
  2002-02-15  8:01   ` Daniel Jacobowitz
  2002-02-15  8:12   ` Daniel Jacobowitz
  1 sibling, 2 replies; 9+ messages in thread
From: Eli Zaretskii @ 2002-01-18  0:17 UTC (permalink / raw)
  To: drow; +Cc: gdb-patches

> Date: Thu, 17 Jan 2002 15:25:04 -0500
> From: Daniel Jacobowitz <drow@mvista.com>
> 
> Someone else did this some time ago, but got lost in the paper trail, I
> think.  This adds '--attach <pid>' as an alternative for 'prog [args...]'. 

How about documenting this in gdb.texinfo?


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [RFA] gdbserver --attach support
  2002-01-18  0:17 ` Eli Zaretskii
@ 2002-02-15  8:01   ` Daniel Jacobowitz
  2002-02-15 10:49     ` Eli Zaretskii
  2002-02-15  8:12   ` Daniel Jacobowitz
  1 sibling, 1 reply; 9+ messages in thread
From: Daniel Jacobowitz @ 2002-02-15  8:01 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: gdb-patches

On Fri, Jan 18, 2002 at 10:14:03AM +0200, Eli Zaretskii wrote:
> > Date: Thu, 17 Jan 2002 15:25:04 -0500
> > From: Daniel Jacobowitz <drow@mvista.com>
> > 
> > Someone else did this some time ago, but got lost in the paper trail, I
> > think.  This adds '--attach <pid>' as an alternative for 'prog [args...]'. 
> 
> How about documenting this in gdb.texinfo?

Well, that took me much too long.  Eli, how's this?  I haven't written
texinfo before, but I think I did OK.

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

2002-02-15  Daniel Jacobowitz  <drow@mvista.com>

	* gdb.texinfo: Document gdbserver ``--attach'' command.

Index: gdb.texinfo
===================================================================
RCS file: /cvs/src/src/gdb/doc/gdb.texinfo,v
retrieving revision 1.89
diff -u -r1.89 gdb.texinfo
--- gdb.texinfo	2002/02/08 00:39:45	1.89
+++ gdb.texinfo	2002/02/15 15:59:44
@@ -9956,7 +9956,7 @@
 system does all the symbol handling.
 
 To use the server, you must tell it how to communicate with @value{GDBN};
-the name of your program; and the arguments for your program.  The
+the name of your program; and the arguments for your program.  The usual
 syntax is:
 
 @smallexample
@@ -9992,6 +9992,16 @@
 conflicts with another service, @code{gdbserver} prints an error message
 and exits.}  You must use the same port number with the host @value{GDBN}
 @code{target remote} command.
+
+On some targets, @code{gdbserver} can also attach to running programs.
+This is accomplished via the @code{--attach} argument.  The syntax is:
+
+@smallexample
+target> gdbserver @var{comm} --attach @var{PID}
+@end smallexample
+
+@var{PID} is the process ID of a currently running process.  It isn't necessary
+to point @code{gdbserver} at a binary for the running process.
 
 @item On the @value{GDBN} host machine,
 you need an unstripped copy of your program, since @value{GDBN} needs


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [RFA] gdbserver --attach support
  2002-01-18  0:17 ` Eli Zaretskii
  2002-02-15  8:01   ` Daniel Jacobowitz
@ 2002-02-15  8:12   ` Daniel Jacobowitz
  2002-02-15 10:51     ` Eli Zaretskii
  1 sibling, 1 reply; 9+ messages in thread
From: Daniel Jacobowitz @ 2002-02-15  8:12 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: gdb-patches

On Fri, Jan 18, 2002 at 10:14:03AM +0200, Eli Zaretskii wrote:
> > Date: Thu, 17 Jan 2002 15:25:04 -0500
> > From: Daniel Jacobowitz <drow@mvista.com>
> > 
> > Someone else did this some time ago, but got lost in the paper trail, I
> > think.  This adds '--attach <pid>' as an alternative for 'prog [args...]'. 
> 
> How about documenting this in gdb.texinfo?

And in the gdbserver man page would be nice too.  OK to commit?

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

2002-02-15  Daniel Jacobowitz  <drow@mvista.com>

	* gdbserver/gdbserver.1: Document --attach.

Index: gdbserver.1
===================================================================
RCS file: /cvs/src/src/gdb/gdbserver/gdbserver.1,v
retrieving revision 1.2
diff -u -r1.2 gdbserver.1
--- gdbserver.1	2001/03/06 08:21:44	1.2
+++ gdbserver.1	2002/02/15 16:10:49
@@ -10,6 +10,11 @@
 .RB tty
 .RB prog
 .RB "[\|" args... "\|]"
+.PP
+.B gdbserver
+.RB tty
+.B --attach
+.RB PID
 .ad b
 .SH DESCRIPTION
 GDBSERVER is a program that allows you to run GDB on a different machine
@@ -49,6 +54,14 @@
 GDBs `target remote' command, which will be described shortly.  Note that if
 you chose a port number that conflicts with another service, gdbserver will
 print an error message and exit.
+
+On some targets, gdbserver can also attach to running programs.
+This is accomplished via the --attach argument.  The syntax is:
+
+	target> gdbserver COMM --attach PID
+
+PID is the process ID of a currently running process.  It isn't
+necessary to point gdbserver at a binary for the running process.
 
 Usage (host side):
 


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [RFA] gdbserver --attach support
  2002-02-15  8:01   ` Daniel Jacobowitz
@ 2002-02-15 10:49     ` Eli Zaretskii
  2002-02-15 11:06       ` Daniel Jacobowitz
  0 siblings, 1 reply; 9+ messages in thread
From: Eli Zaretskii @ 2002-02-15 10:49 UTC (permalink / raw)
  To: drow; +Cc: gdb-patches

> Date: Fri, 15 Feb 2002 11:01:24 -0500
> From: Daniel Jacobowitz <drow@mvista.com>
> 
> Well, that took me much too long.  Eli, how's this?  I haven't written
> texinfo before, but I think I did OK.

You did very well.  Allow me a minor comment:

> +@smallexample
> +target> gdbserver @var{comm} --attach @var{PID}
> +@end smallexample

There's no need to up-case the argument of @var.  In the Info manual,
it will be up-cased by makeinfo.  But in the printed manual, TeX
typesets @var in a slanted typeface, so upper-case PID will look ugly.
The same pertains to the HTML output.

So just @var{pid} should do the job.

Otherwise, please commit this.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [RFA] gdbserver --attach support
  2002-02-15  8:12   ` Daniel Jacobowitz
@ 2002-02-15 10:51     ` Eli Zaretskii
  0 siblings, 0 replies; 9+ messages in thread
From: Eli Zaretskii @ 2002-02-15 10:51 UTC (permalink / raw)
  To: drow; +Cc: gdb-patches

> Date: Fri, 15 Feb 2002 11:11:58 -0500
> From: Daniel Jacobowitz <drow@mvista.com>
> 
> And in the gdbserver man page would be nice too.  OK to commit?

Yes, thanks.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [RFA] gdbserver --attach support
  2002-02-15 10:49     ` Eli Zaretskii
@ 2002-02-15 11:06       ` Daniel Jacobowitz
  0 siblings, 0 replies; 9+ messages in thread
From: Daniel Jacobowitz @ 2002-02-15 11:06 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: gdb-patches

On Fri, Feb 15, 2002 at 08:46:35PM +0200, Eli Zaretskii wrote:
> > Date: Fri, 15 Feb 2002 11:01:24 -0500
> > From: Daniel Jacobowitz <drow@mvista.com>
> > 
> > Well, that took me much too long.  Eli, how's this?  I haven't written
> > texinfo before, but I think I did OK.
> 
> You did very well.  Allow me a minor comment:
> 
> > +@smallexample
> > +target> gdbserver @var{comm} --attach @var{PID}
> > +@end smallexample
> 
> There's no need to up-case the argument of @var.  In the Info manual,
> it will be up-cased by makeinfo.  But in the printed manual, TeX
> typesets @var in a slanted typeface, so upper-case PID will look ugly.
> The same pertains to the HTML output.
> 
> So just @var{pid} should do the job.
> 
> Otherwise, please commit this.

Learn something every day... Thanks!

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


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2002-02-15 19:06 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-01-17 12:25 [RFA] gdbserver --attach support Daniel Jacobowitz
2002-01-17 12:31 ` Andrew Cagney
2002-01-17 12:46   ` Daniel Jacobowitz
2002-01-18  0:17 ` Eli Zaretskii
2002-02-15  8:01   ` Daniel Jacobowitz
2002-02-15 10:49     ` Eli Zaretskii
2002-02-15 11:06       ` Daniel Jacobowitz
2002-02-15  8:12   ` Daniel Jacobowitz
2002-02-15 10:51     ` Eli Zaretskii

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox