Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Pedro Alves <palves@redhat.com>
To: gdb-patches@sourceware.org
Subject: [PATCH] PR server/9684: gdbserver, attach to stopped processes
Date: Fri, 24 Feb 2012 23:00:00 -0000	[thread overview]
Message-ID: <20120224222127.14659.51317.stgit@hit-nxdomain.opendns.com> (raw)

This teaches GDBserver to attach to job control stopped processes 'T
(stopped)', using the same trick GDB uses.

Refs:
 http://sourceware.org/ml/gdb-patches/2008-04/msg00241.html
 http://sourceware.org/ml/gdb-patches/2008-04/msg00028.html
 http://sourceware.org/ml/gdb-patches/2007-06/msg00059.html

This patch is simpler than linux-nat's equivalent patch was, given
that GDBserver doesn't block waiting for lwps to report the attach
stop.  Instead all that is handled through the regular target_wait
path.  That and that linux-nat.c's state at the time of that patch was
a little messy.  :-)

I'm a bit ambivalent on this.  I found out that this isn't actually
necessary on recent kernels (I haven't looked for the exact version or
commit that made it so) -- waitpid no longer hangs.  OTOH, we still
need it on systems with not so recent kernels where support is still
well active, so I figure this may be useful to many upstream as well.
Note that once we have PTRACE_SEIZE support this hack will definitely
be unnecessary, so it's likely that the window of kernels where this
is actually unnecessary, but used, will be narrow-ish (assuming I or
someone else gets to PTRACE_SEIZE in reasonable time).

Tested with the extended-remote board on x86_64 Fedora 16 (3.2,
doesn't need patch), and x86_64 RHEL 5.8 (2.6.18, needs patch).

2012-02-24  Pedro Alves  <palves@redhat.com>

	PR server/9684
	* linux-low.c (pid_is_stopped): New.
	(linux_attach_lwp_1): Handle attaching to 'T (stopped)' processes.
---
 0 files changed, 0 insertions(+), 0 deletions(-)

diff --git a/gdb/gdbserver/linux-low.c b/gdb/gdbserver/linux-low.c
index ab34d84..11c53e6 100644
--- a/gdb/gdbserver/linux-low.c
+++ b/gdb/gdbserver/linux-low.c
@@ -598,6 +598,37 @@ linux_create_inferior (char *program, char **allargs)
   return pid;
 }
 
+/* Detect `T (stopped)' in `/proc/PID/status'.
+   Other states including `T (tracing stop)' are reported as false.  */
+
+static int
+pid_is_stopped (pid_t pid)
+{
+  FILE *status_file;
+  char buf[100];
+  int retval = 0;
+
+  snprintf (buf, sizeof (buf), "/proc/%d/status", (int) pid);
+  status_file = fopen (buf, "r");
+  if (status_file != NULL)
+    {
+      int have_state = 0;
+
+      while (fgets (buf, sizeof (buf), status_file))
+	{
+	  if (strncmp (buf, "State:", 6) == 0)
+	    {
+	      have_state = 1;
+	      break;
+	    }
+	}
+      if (have_state && strstr (buf, "T (stopped)") != NULL)
+	retval = 1;
+      fclose (status_file);
+    }
+  return retval;
+}
+
 /* Attach to an inferior process.  */
 
 static void
@@ -643,6 +674,33 @@ linux_attach_lwp_1 (unsigned long lwpid, int initial)
      ptrace call on this LWP.  */
   new_lwp->must_set_ptrace_flags = 1;
 
+  if (pid_is_stopped (lwpid))
+    {
+      if (debug_threads)
+	fprintf (stderr,
+		 "Attached to a stopped process\n");
+
+      /* The process is definitely stopped.  It is in a job control
+	 stop, unless the kernel predates the TASK_STOPPED /
+	 TASK_TRACED distinction, in which case it might be in a
+	 ptrace stop.  Make sure it is in a ptrace stop; from there we
+	 can kill it, signal it, et cetera.
+
+         First make sure there is a pending SIGSTOP.  Since we are
+	 already attached, the process can not transition from stopped
+	 to running without a PTRACE_CONT; so we know this signal will
+	 go into the queue.  The SIGSTOP generated by PTRACE_ATTACH is
+	 probably already in the queue (unless this kernel is old
+	 enough to use TASK_STOPPED for ptrace stops); but since
+	 SIGSTOP is not an RT signal, it can only be queued once.  */
+      kill_lwp (lwpid, SIGSTOP);
+
+      /* Finally, resume the stopped process.  This will deliver the
+	 SIGSTOP (or a higher priority signal, just like normal
+	 PTRACE_ATTACH), which we'll catch later on.  */
+      ptrace (PTRACE_CONT, lwpid, 0, 0);
+    }
+
   /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
      brings it to a halt.
 


             reply	other threads:[~2012-02-24 22:21 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-24 23:00 Pedro Alves [this message]
2012-02-24 23:22 ` Joel Brobecker
2012-02-27 17:26   ` Pedro Alves
2012-02-25  5:58 ` Jan Kratochvil
2012-02-27 16:24   ` Pedro Alves
2012-02-25  6:24 ` Yao Qi
2012-02-27 16:31   ` move pid_is_stopped to common/ Pedro Alves

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120224222127.14659.51317.stgit@hit-nxdomain.opendns.com \
    --to=palves@redhat.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox