[PATCH 4/4] Add conditionally enabled PTRACE_SEIZE support
Denys Vlasenko
dvlasenk at redhat.com
Fri Sep 2 14:18:13 UTC 2011
> Patch 4: Add conditionally enabled SEIZE support.
diff -d -urpN strace.4/defs.h strace.5/defs.h
--- strace.4/defs.h 2011-09-02 11:23:45.242545628 +0200
+++ strace.5/defs.h 2011-09-02 14:48:08.945064165 +0200
@@ -383,6 +383,23 @@ extern int mp_ioctl(int f, int c, void *
# if !HAVE_DECL_PTRACE_EVENT_EXIT
# define PTRACE_EVENT_EXIT 6
# endif
+
+/* Experimental code using PTRACE_SEIZE can be enabled here: */
+//# define USE_SEIZE 1
+
+# ifdef USE_SEIZE
+# undef PTRACE_SEIZE
+# define PTRACE_SEIZE 0x4206
+# undef PTRACE_INTERRUPT
+# define PTRACE_INTERRUPT 0x4207
+# undef PTRACE_LISTEN
+# define PTRACE_LISTEN 0x4208
+# undef PTRACE_SEIZE_DEVEL
+# define PTRACE_SEIZE_DEVEL 0x80000000
+# undef PTRACE_EVENT_STOP
+# define PTRACE_EVENT_STOP 7
+# endif
+
#endif /* LINUX */
#if !defined __GNUC__
diff -d -urpN strace.4/strace.c strace.5/strace.c
--- strace.4/strace.c 2011-09-02 16:06:21.864293092 +0200
+++ strace.5/strace.c 2011-09-02 15:13:34.464884693 +0200
@@ -101,6 +101,14 @@ static int iflag = 0, interactive = 0, p
*/
static bool daemonized_tracer = 0;
+#ifdef USE_SEIZE
+static int post_attach_sigstop = TCB_IGNORE_ONE_SIGSTOP;
+# define use_seize (post_attach_sigstop == 0)
+#else
+# define post_attach_sigstop TCB_IGNORE_ONE_SIGSTOP
+# define use_seize 0
+#endif
+
/* Sometimes we want to print only succeeding syscalls. */
int not_failing_only = 0;
@@ -290,6 +298,23 @@ foobar()
# define fork() vfork()
#endif
+#ifdef USE_SEIZE
+static int
+ptrace_attach_or_seize(int pid)
+{
+ int r;
+ if (!use_seize)
+ return ptrace(PTRACE_ATTACH, pid, 0, 0);
+ r = ptrace(PTRACE_SEIZE, pid, 0, PTRACE_SEIZE_DEVEL);
+ if (r)
+ return r;
+ r = ptrace(PTRACE_INTERRUPT, pid, 0, 0);
+ return r;
+}
+#else
+# define ptrace_attach_or_seize(pid) ptrace(PTRACE_ATTACH, (pid), 0, 0)
+#endif
+
static void
set_cloexec_flag(int fd)
{
@@ -480,7 +505,7 @@ startup_attach(void)
if (tid <= 0)
continue;
++ntid;
- if (ptrace(PTRACE_ATTACH, tid, (char *) 1, 0) < 0) {
+ if (ptrace_attach_or_seize(tid) < 0) {
++nerr;
if (debug)
fprintf(stderr, "attach to pid %d failed\n", tid);
@@ -491,7 +516,7 @@ startup_attach(void)
cur_tcp = tcp;
if (tid != tcp->pid)
cur_tcp = alloctcb(tid);
- cur_tcp->flags |= TCB_ATTACHED | TCB_STARTUP | TCB_IGNORE_ONE_SIGSTOP;
+ cur_tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
}
closedir(dir);
if (interactive) {
@@ -523,12 +548,12 @@ startup_attach(void)
} /* if (opendir worked) */
} /* if (-f) */
# endif /* LINUX */
- if (ptrace(PTRACE_ATTACH, tcp->pid, (char *) 1, 0) < 0) {
+ if (ptrace_attach_or_seize(tcp->pid) < 0) {
perror("attach: ptrace(PTRACE_ATTACH, ...)");
droptcb(tcp);
continue;
}
- tcp->flags |= TCB_STARTUP | TCB_IGNORE_ONE_SIGSTOP;
+ tcp->flags |= TCB_STARTUP | post_attach_sigstop;
if (debug)
fprintf(stderr, "attach to pid %d (main) succeeded\n", tcp->pid);
@@ -644,12 +669,10 @@ startup_child(char **argv)
kill(pid, SIGSTOP);
# endif
#else /* !USE_PROCFS */
- if (!daemonized_tracer) {
+ if (!daemonized_tracer && !use_seize) {
if (ptrace(PTRACE_TRACEME, 0, (char *) 1, 0) < 0) {
perror_msg_and_die("ptrace(PTRACE_TRACEME, ...)");
}
- if (debug)
- kill(pid, SIGSTOP);
}
if (username != NULL) {
@@ -713,9 +736,32 @@ startup_child(char **argv)
/* We are the tracer */
if (!daemonized_tracer) {
+///
+ if (use_seize) {
+ if (!strace_vforked) {
+ /* Wait until child stopped itself */
+ int status;
+ while (waitpid(pid, &status, WSTOPPED) < 0) {
+ if (errno == EINTR)
+ continue;
+ perror_msg_and_die("waitpid");
+ }
+ if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP) {
+ kill(pid, SIGKILL);
+ perror_msg_and_die("Unexpected wait status %x", status);
+ }
+fprintf(stderr, "Pid %d stopped, attaching\n", pid);
+ }
+ if (ptrace_attach_or_seize(pid)) {
+ kill(pid, SIGKILL);
+ perror_msg_and_die("Can't attach to %d", pid);
+ }
+ if (!strace_vforked)
+ kill(pid, SIGCONT);
+ }
tcp = alloctcb(pid);
if (!strace_vforked)
- tcp->flags |= TCB_STARTUP | TCB_IGNORE_ONE_SIGSTOP;
+ tcp->flags |= TCB_STARTUP | post_attach_sigstop;
else
tcp->flags |= TCB_STARTUP;
}
@@ -943,6 +989,55 @@ test_ptrace_setoptions_for_all(void)
error_msg("Test for PTRACE_O_TRACESYSGOOD failed, "
"giving up using this feature.");
}
+
+# ifdef USE_SEIZE
+static void
+test_ptrace_seize(void)
+{
+ int pid;
+
+ pid = fork();
+ if (pid < 0)
+ perror_msg_and_die("fork");
+
+ if (pid == 0) {
+ pause();
+ _exit(0);
+ }
+
+ /* PTRACE_SEIZE, unlike ATTACH, doesn't force tracee to trap. After
+ * attaching tracee continues to run unless a trap condition occurs.
+ * PTRACE_SEIZE doesn't affect signal or group stop state.
+ */
+ if (ptrace(PTRACE_SEIZE, pid, 0, PTRACE_SEIZE_DEVEL) == 0) {
+ post_attach_sigstop = 0; /* this sets use_seize to 1 */
+ }
+else fprintf(stderr, "PTRACE_SEIZE doesn't work\n");
+
+ kill(pid, SIGKILL);
+
+ while (1) {
+ int status, tracee_pid;
+
+ errno = 0;
+ tracee_pid = waitpid(pid, &status, 0);
+ if (tracee_pid <= 0) {
+ if (errno == EINTR)
+ continue;
+ perror_msg_and_die("%s: unexpected wait result %d",
+ __func__, tracee_pid);
+ }
+ if (WIFSIGNALED(status)) {
+ return;
+ }
+ error_msg_and_die("%s: unexpected wait status %x",
+ __func__, status);
+ }
+}
+# else /* !USE_SEIZE */
+# define test_ptrace_seize() ((void)0)
+# endif
+
#endif
int
@@ -1143,6 +1238,7 @@ main(int argc, char *argv[])
if (followfork)
test_ptrace_setoptions_followfork();
test_ptrace_setoptions_for_all();
+ test_ptrace_seize();
#endif
/* Check if they want to redirect the output. */
@@ -1687,7 +1783,7 @@ detach(struct tcb *tcp, int sig)
#define PTRACE_DETACH PTRACE_SUNDETACH
#endif
/*
- * We did PTRACE_ATTACH but possibly didn't see the expected SIGSTOP.
+ * We attached but possibly didn't see the expected SIGSTOP.
* We must catch exactly one as otherwise the detached process
* would be left stopped (process state T).
*/
@@ -2421,7 +2517,7 @@ trace()
child so that we know how to do clearbpt
in the child. */
tcp = alloctcb(pid);
- tcp->flags |= TCB_ATTACHED | TCB_STARTUP | TCB_IGNORE_ONE_SIGSTOP;
+ tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
if (!qflag)
fprintf(stderr, "Process %d attached\n",
pid);
@@ -2540,8 +2636,12 @@ trace()
}
if (sig != syscall_trap_sig) {
+ siginfo_t si;
+ int stopped;
+
if (sig == SIGSTOP &&
(tcp->flags & TCB_SIGTRAPPED)) {
+//TODO: testcase.
/*
* Trapped attempt to block SIGTRAP
* Hope we are back in control now.
@@ -2549,9 +2649,14 @@ trace()
tcp->flags &= ~(TCB_INSYSCALL | TCB_SIGTRAPPED);
goto restart_tracee_with_sig_0;
}
+
+ /* Nonzero (true) if tracee is stopped by signal
+ * (as opposed to "tracee received signal").
+ */
+ stopped = ptrace(PTRACE_GETSIGINFO, pid, 0, &si);
+
if (cflag != CFLAG_ONLY_STATS
&& (qual_flags[sig] & QUAL_SIGNAL)) {
- siginfo_t si;
#if defined(PT_CR_IPSR) && defined(PT_CR_IIP)
long pc = 0;
long psr = 0;
@@ -2568,7 +2673,7 @@ trace()
# define PC_FORMAT_ARG /* nothing */
#endif
printleader(tcp);
- if (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) == 0) {
+ if (!stopped) {
tprints("--- ");
printsiginfo(&si, verbose(tcp));
tprintf(" (%s)" PC_FORMAT_STR " ---",
@@ -2581,6 +2686,26 @@ trace()
PC_FORMAT_ARG);
printtrailer();
}
+ if (!stopped)
+ /* It's signal-delivery-stop. Inject the signal */
+ goto restart_tracee;
+
+ /* It's group-stop */
+#ifdef USE_SEIZE
+ if (use_seize) {
+ /*
+ * This ends ptrace-stop, but does *not* end group-stop.
+ * This makes stopping signals work properly on straced process
+ * (that is, process really stops. It used to continue to run).
+ */
+ if (ptrace_restart(PTRACE_LISTEN, tcp, 0) < 0) {
+ cleanup();
+ return -1;
+ }
+ continue;
+ }
+ /* We don't have PTRACE_LISTEN support... */
+#endif
goto restart_tracee;
}
diff -d -urpN strace.4/util.c strace.5/util.c
--- strace.4/util.c 2011-09-02 11:42:19.753317922 +0200
+++ strace.5/util.c 2011-09-02 11:42:25.706451203 +0200
@@ -224,6 +224,10 @@ ptrace_restart(int op, struct tcb *tcp,
msg = "CONT";
if (op == PTRACE_DETACH)
msg = "DETACH";
+#ifdef PTRACE_LISTEN
+ if (op == PTRACE_LISTEN)
+ msg = "LISTEN";
+#endif
perror_msg("ptrace(PTRACE_%s,1,%d)", msg, sig);
return -1;
}
More information about the Strace-devel
mailing list