In my project I need to attach to processes, resume them and later detach using ptrace
. However, detaching fails with errno=ESRCH (No such process)
.
Detaching works fine if I don't resume the process with PTRACE_CONT
, but in this case the process is stopped/unresponsive which is not acceptable in my project. Tested on Arch & Ubuntu 12.04 LTS with identical results.
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
int main(int argc, char *argv[])
{
pid_t pid = 21000;
if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) == -1) {
perror("PTRACE_ATTACH");
return 1;
}
printf("attached\n");
waitpid(pid, NULL, WUNTRACED);
if (ptrace(PTRACE_CONT, pid, NULL, NULL) == -1) {
perror("PTRACE_CONT");
return 1;
}
printf("continued\n");
if (ptrace(PTRACE_DETACH, pid, NULL, NULL) == -1) {
perror("PTRACE_DETACH");
return 1;
}
printf("detached\n");
return 0;
}
The output:
attached
continued
PTRACE_DETACH: No such process
According to
ptrace
man page the process should be stopped before trying to detach from it:In your example code there is actually no need to call
ptrace(PTRACE_CONT, ...)
. You can just detach from the process. If that code belongs to a larger piece of code, then you can just usetgkill()
(or simplykill
if you are not using threads):