ptrace attach/detach timing problem. - Aix

This is a discussion on ptrace attach/detach timing problem. - Aix ; I'm getting an ESRCH (no such process) error from ptrace calls (PT_READ_D and PT_DETACH) after an appearantly successful PT_ATTACH. And the target process is killed (signal 9, based on it's exit code in $?). Here is my small test case ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: ptrace attach/detach timing problem.

  1. ptrace attach/detach timing problem.

    I'm getting an ESRCH (no such process) error from ptrace calls
    (PT_READ_D and PT_DETACH) after an appearantly successful PT_ATTACH.
    And the target process is killed (signal 9, based on it's exit code in
    $?).

    Here is my small test case that attaches and detaches from a process.

    int main (int argc, char *argv[])
    {
    pid_t p = atoi(argv[1]);
    int rc=ptrace(PT_ATTACH, p, 0, 0, 0);
    printf("%d, %d, %d\n",rc,errno,p);
    int drc=ptrace(PT_DETACH, p, (int*)SIGCONT, 0, 0);
    printf("%d, %d, %d\n",drc,errno,p);
    return 0;
    }

    I start a ksh in another session (pid=665082).

    chris@wally>a.out 665082
    0, 0, 665082
    0, 0, 665082
    chris@wally>a.out 665082
    0, 0, 665082
    -1, 3, 665082

    the $? from pid 665082's death is 137. sometimes this happens the
    first time i run the testcase against a process and sometimes it works
    several times before failing. the target being killed with -9 makes
    sense if you consider that the attach really was successful, the
    detach failed and the test case exits. (taking it's debug target with
    it, just like quit in dbx).

    If I add a sleep(1) between the attach and detach calls, all seems
    fine. Has anyone seen this before or have an explanation, since
    problems solved by "sleep", are not really solved, just hidden.

    This is aix 5200-01, btw.

    Thanks,


  2. Re: ptrace attach/detach timing problem.

    I think I found my own answer. The man page mentioned this:

    >A debugged process runs normally until it encounters a signal.
    >Then it enters a stopped state and its debugging process is
    >notified with the wait subroutine.


    So i tried adding a waitpid() call against the target and now it works
    every time.

    Turns out that's what dbx does when it attaches to something too. (as
    seen through truss).



+ Reply to Thread