truss(1): detach more carefully

When detaching, truss(1) sends SIGSTOP to the traced process to ensure
that it is detaching in the steady state.  But it is possible, for
multithreaded process, that wait() call returns event other than our
SIGSTOP notification.  As result, SIGSTOP might sit in some thread'
sigqueue, which makes SIGCONT a nop.  Then, the process is stopped when
the queued SIGSTOP is acted upon.

To handle this, loop until we drain everything before SIGSTOP,
and see that the process is stopped.

Note that the earlier fix makes it safe to have some more debugging
events longering after SIGSTOP is acted upon.  They will be ignored
after PT_DETACH.

Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D33861
This commit is contained in:
Konstantin Belousov 2022-01-12 10:21:19 +02:00
parent ba33c28848
commit 12f747e6ff

View File

@ -205,11 +205,24 @@ restore_proc(int signo __unused)
static void
detach_proc(pid_t pid)
{
int sig, status;
/* stop the child so that we can detach */
/*
* Stop the child so that we can detach. Filter out possible
* lingering SIGTRAP events buffered in the threads.
*/
kill(pid, SIGSTOP);
if (waitpid(pid, NULL, 0) < 0)
err(1, "Unexpected stop in waitpid");
for (;;) {
if (waitpid(pid, &status, 0) < 0)
err(1, "Unexpected error in waitpid");
sig = WIFSTOPPED(status) ? WSTOPSIG(status) : 0;
if (sig == SIGSTOP)
break;
if (sig == SIGTRAP)
sig = 0;
if (ptrace(PT_CONTINUE, pid, (caddr_t)1, sig) < 0)
err(1, "Can not continue for detach");
}
if (ptrace(PT_DETACH, pid, (caddr_t)1, 0) < 0)
err(1, "Can not detach the process");