3

I am practicing using ptrace but I don’t know much about the relationship between it and waitpid.

This is my test program:

int main(int argc, char *argv[])
{
    pid_t pid = 22092;
    if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) == -1) {
        perror("PTRACE_ATTACH");
        return 1;
    } 
    
    //waitpid(pid, NULL, WUNTRACED);
     
    if (ptrace(PTRACE_DETACH, pid, NULL, NULL) == -1) {
        perror("PTRACE_DETACH");
        return 1;
    }
    
    return 0;
}

If I mark waitpid(pid, NULL, WUNTRACED), there will be (no such process) problem, but if waitpid is added, it can be executed normally. I want to ask what causes it.

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
mark25789
  • 43
  • 3
  • When you say, "If I mark", do you mean, "If I comment"? – Daniel Walker Aug 16 '22 at 14:31
  • 1
    Where will this "no such process" problem arise? Is it when you try to detach from a process you know has exited? Be specific about where this error happens. – Useless Aug 16 '22 at 14:37
  • 1
    `waitpid()` reports only about the calling process's children. It must always report an `ECHILD` error (or perhaps a different error) if called by a process that has never forked any children. This has nothing to do with the `WUNTRACED` flag. – John Bollinger Aug 16 '22 at 14:50

0 Answers0