Tejun Heo 544b2c91a9 ptrace: implement PTRACE_LISTEN
The previous patch implemented async notification for ptrace but it
only worked while trace is running.  This patch introduces
PTRACE_LISTEN which is suggested by Oleg Nestrov.

It's allowed iff tracee is in STOP trap and puts tracee into
quasi-running state - tracee never really runs but wait(2) and
ptrace(2) consider it to be running.  While ptracer is listening,
tracee is allowed to re-enter STOP to notify an async event.
Listening state is cleared on the first notification.  Ptracer can
also clear it by issuing INTERRUPT - tracee will re-trap into STOP
with listening state cleared.

This allows ptracer to monitor group stop state without running tracee
- use INTERRUPT to put tracee into STOP trap, issue LISTEN and then
wait(2) to wait for the next group stop event.  When it happens,
PTRACE_GETSIGINFO provides information to determine the current state.

Test program follows.

  #define PTRACE_SEIZE		0x4206
  #define PTRACE_INTERRUPT	0x4207
  #define PTRACE_LISTEN		0x4208

  #define PTRACE_SEIZE_DEVEL	0x80000000

  static const struct timespec ts1s = { .tv_sec = 1 };

  int main(int argc, char **argv)
  {
	  pid_t tracee, tracer;
	  int i;

	  tracee = fork();
	  if (!tracee)
		  while (1)
			  pause();

	  tracer = fork();
	  if (!tracer) {
		  siginfo_t si;

		  ptrace(PTRACE_SEIZE, tracee, NULL,
			 (void *)(unsigned long)PTRACE_SEIZE_DEVEL);
		  ptrace(PTRACE_INTERRUPT, tracee, NULL, NULL);
	  repeat:
		  waitid(P_PID, tracee, NULL, WSTOPPED);

		  ptrace(PTRACE_GETSIGINFO, tracee, NULL, &si);
		  if (!si.si_code) {
			  printf("tracer: SIG %d\n", si.si_signo);
			  ptrace(PTRACE_CONT, tracee, NULL,
				 (void *)(unsigned long)si.si_signo);
			  goto repeat;
		  }
		  printf("tracer: stopped=%d signo=%d\n",
			 si.si_signo != SIGTRAP, si.si_signo);
		  if (si.si_signo != SIGTRAP)
			  ptrace(PTRACE_LISTEN, tracee, NULL, NULL);
		  else
			  ptrace(PTRACE_CONT, tracee, NULL, NULL);
		  goto repeat;
	  }

	  for (i = 0; i < 3; i++) {
		  nanosleep(&ts1s, NULL);
		  printf("mother: SIGSTOP\n");
		  kill(tracee, SIGSTOP);
		  nanosleep(&ts1s, NULL);
		  printf("mother: SIGCONT\n");
		  kill(tracee, SIGCONT);
	  }
	  nanosleep(&ts1s, NULL);

	  kill(tracer, SIGKILL);
	  kill(tracee, SIGKILL);
	  return 0;
  }

This is identical to the program to test TRAP_NOTIFY except that
tracee is PTRACE_LISTEN'd instead of PTRACE_CONT'd when group stopped.
This allows ptracer to monitor when group stop ends without running
tracee.

  # ./test-listen
  tracer: stopped=0 signo=5
  mother: SIGSTOP
  tracer: SIG 19
  tracer: stopped=1 signo=19
  mother: SIGCONT
  tracer: stopped=0 signo=5
  tracer: SIG 18
  mother: SIGSTOP
  tracer: SIG 19
  tracer: stopped=1 signo=19
  mother: SIGCONT
  tracer: stopped=0 signo=5
  tracer: SIG 18
  mother: SIGSTOP
  tracer: SIG 19
  tracer: stopped=1 signo=19
  mother: SIGCONT
  tracer: stopped=0 signo=5
  tracer: SIG 18

-v2: Moved JOBCTL_LISTENING check in wait_task_stopped() into
     task_stopped_code() as suggested by Oleg.

Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Oleg Nesterov <oleg@redhat.com>
2011-06-16 21:41:54 +02:00
..
2011-03-31 11:26:23 -03:00
2011-05-04 14:08:36 -07:00
2011-03-31 11:26:23 -03:00
2011-05-26 19:45:40 +02:00
2011-03-31 11:26:23 -03:00
2011-06-01 11:36:49 +01:00
2011-03-31 11:26:23 -03:00
2011-03-31 11:26:23 -03:00
2011-03-31 11:26:23 -03:00
2011-03-31 11:26:23 -03:00
2011-02-24 19:58:42 +01:00
2010-10-15 21:18:59 +02:00
2011-04-01 02:24:31 -04:00
2010-11-23 20:14:46 +00:00
2011-01-31 14:03:00 -08:00
2011-05-28 17:41:46 +02:00
2011-01-15 20:07:45 -05:00
2011-03-10 08:52:07 +01:00
2011-03-22 17:43:59 -07:00
2011-03-11 14:25:50 +00:00
2011-05-31 13:45:53 +02:00
2011-03-31 11:26:23 -03:00
2010-12-16 17:53:38 +01:00
2011-03-31 11:26:23 -03:00
2011-05-26 17:12:34 -07:00
2011-05-26 17:12:34 -07:00
2011-03-31 11:26:23 -03:00
2010-10-25 08:02:40 -07:00
2011-03-31 11:26:23 -03:00
2011-03-31 11:26:23 -03:00
2011-03-22 17:44:15 -07:00
2011-05-19 15:59:38 -07:00
2011-01-10 08:51:44 -08:00
2010-08-04 11:00:45 +02:00
2011-05-23 10:47:06 -05:00
2011-05-29 13:03:09 +01:00
2011-03-31 11:26:23 -03:00
2011-03-31 11:26:23 -03:00
2011-03-10 11:35:17 +01:00
2011-05-24 10:21:29 +02:00
2011-05-24 10:21:29 +02:00
2010-10-21 14:47:59 +02:00
2011-05-13 14:37:28 -04:00
2011-03-31 11:26:23 -03:00
2011-03-31 11:26:23 -03:00
2011-01-12 20:16:43 -05:00
2011-03-14 19:12:20 -04:00
2010-08-19 17:18:03 -07:00
2011-03-31 11:26:23 -03:00
2011-01-10 08:51:44 -08:00
2011-01-07 17:50:27 +11:00
2011-03-31 11:26:23 -03:00
2011-01-07 17:50:23 +11:00
2010-12-07 20:16:56 +01:00
2010-10-25 14:11:37 -07:00
2011-03-05 10:56:00 +01:00
2011-03-31 11:26:23 -03:00
2011-03-31 11:26:23 -03:00
2011-05-23 13:59:54 +02:00
2010-10-12 16:53:37 +02:00
2011-05-26 12:03:50 -07:00
2011-02-23 00:53:26 +00:00
2011-05-25 20:43:32 +02:00
2011-03-31 11:26:23 -03:00
2011-05-08 16:41:45 -07:00
2010-11-16 10:58:30 -08:00
2010-09-21 18:04:47 -07:00
2011-02-27 16:11:51 -08:00
2010-08-19 17:16:23 -07:00
2010-10-26 16:52:08 -07:00
2010-08-09 20:45:05 -07:00
2011-05-26 17:12:37 -07:00
2011-03-31 11:26:23 -03:00
2010-11-24 11:16:42 -08:00
2010-10-07 14:08:55 +01:00
2011-03-31 11:26:23 -03:00
2010-08-10 11:49:21 -07:00
2010-12-09 20:17:07 -08:00
2011-03-31 11:26:23 -03:00
2010-08-12 11:27:58 +02:00
2011-02-17 11:12:40 -08:00
2010-10-22 15:34:12 -05:00
2011-01-13 17:32:31 -08:00
2011-05-25 08:39:52 -07:00
2011-01-13 17:32:47 -08:00
2011-02-13 16:54:24 -08:00
2011-01-24 14:45:11 +10:30
2011-05-24 22:55:24 +10:00
2010-09-09 18:57:24 -07:00
2011-03-31 11:26:23 -03:00
2011-05-22 08:47:53 -04:00
2011-03-31 11:26:23 -03:00
2011-05-19 20:50:53 -04:00
2011-05-24 14:33:35 +02:00
2011-04-25 18:14:10 -07:00
2010-12-06 11:03:46 -08:00
2011-05-29 11:32:28 -07:00
2011-05-26 17:12:36 -07:00
2011-05-19 16:55:27 +09:30
2011-01-16 13:47:07 -05:00
2011-03-31 11:26:23 -03:00
2010-10-12 16:53:34 +02:00
2011-05-20 11:46:11 -07:00
2011-03-23 15:29:04 -04:00
2011-05-29 20:54:36 +03:00
2011-05-26 17:12:34 -07:00
2011-04-20 17:01:19 +10:00
2011-03-31 11:26:23 -03:00
2011-03-31 11:26:23 -03:00
2011-01-16 13:47:07 -05:00
2010-10-24 15:07:11 -07:00
2011-05-26 17:12:37 -07:00
2010-08-21 23:40:14 -07:00
2011-03-11 15:13:26 -05:00
2010-07-19 01:58:48 +02:00
2011-03-31 11:26:23 -03:00
2011-05-24 12:10:51 +02:00
2010-08-04 21:53:17 -07:00
2011-01-13 08:03:21 -08:00
2011-03-31 11:26:23 -03:00
2011-05-13 16:31:00 -07:00
2011-06-16 21:41:54 +02:00
2011-03-31 11:26:23 -03:00
2010-10-29 04:16:31 -04:00
2011-01-14 02:36:43 +00:00
2011-05-05 23:16:59 -07:00
2010-09-08 18:16:55 -07:00
2010-08-09 16:47:27 -04:00
2011-03-31 11:26:23 -03:00
2010-11-15 13:24:06 -05:00
2011-05-25 08:39:19 -07:00
2011-06-16 21:41:54 +02:00
2010-10-30 12:12:50 +02:00
2011-05-24 15:22:17 +02:00
2011-03-31 11:26:23 -03:00
2011-05-26 13:38:58 +10:00
2011-05-05 11:10:14 -07:00
2011-03-31 11:26:23 -03:00
2011-03-31 11:26:23 -03:00
2010-08-09 16:48:44 -04:00
2011-03-31 11:26:23 -03:00
2010-08-30 13:23:33 -07:00
2011-02-28 18:00:31 -08:00
2011-02-02 15:28:18 +01:00
2010-09-09 20:41:20 +02:00
2010-11-29 08:55:25 +11:00
2010-11-29 08:55:22 +11:00
2011-03-31 11:26:23 -03:00
2010-12-20 09:37:33 +01:00
2011-03-31 11:26:23 -03:00
2011-03-31 11:26:23 -03:00
2011-05-30 11:14:16 +09:30
2011-03-31 11:26:23 -03:00
2011-03-31 11:26:23 -03:00
2011-01-13 08:03:24 -08:00