Hi!

 there is a problem in ptrace code or my understanding of how
 it should work. man page says taht PT_DETACH acts same way
 PT_CONTIUNE does, but when i try to detach from process with
 PT_DETACH delayed? sigstop is delivered, and process becomes
 suspended. Valid solution/workaround seems to be in calling
 PT_CONTINUE with sigcont, and PT_DETACH after it.

 Example is attached. Please cc me a reply:)
 Thanks


#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ptrace.h>
#include <machine/reg.h>
#include <sys/wait.h>
#include <signal.h>
#include <errno.h>
#include <err.h>

#define SIG(x) [SIG##x] "SIG"#x

char *sigtable[] = {
 SIG(HUP), SIG(INT), SIG(QUIT), SIG(ILL),
 SIG(ABRT), SIG(FPE), SIG(KILL), SIG(SEGV),
 SIG(PIPE), SIG(ALRM), SIG(TERM), SIG(USR1),
 SIG(USR2), SIG(CHLD), SIG(CONT), SIG(STOP),
 SIG(TSTP), SIG(TTIN), SIG(TTOU), SIG(BUS),
 SIG(XCPU), SIG(XFSZ)
};

void show (int status)
{
        if (WIFEXITED (status))
                printf ("ex %d\n", WEXITSTATUS(status));
        else if (WIFSIGNALED (status))
                printf ("ts %s\n", sigtable[WTERMSIG(status)]);
        else if (WIFSTOPPED (status))
                printf ("ss %s\n", sigtable[WSTOPSIG(status)]);
        return;
}

int main (int argc, char *argv[])
{
        struct reg regs;
        int status;
        pid_t pid;

        if (argc != 2) exit(1);
        pid = atoi (argv[1]);

        if (ptrace (PT_ATTACH, pid, 0, SIGCONT))
                err (1, "ptrace attach");
        while (wait4(-1, &status, WUNTRACED, NULL) != pid);
        show (status);
        if (ptrace (PT_GETREGS, pid, &regs, NULL))
                err (1, "ptace getregs");
        printf ("attach ok, pc: %#lx\n", regs.r_eip);
        /* uncomment this , it will wokr
        ptrace (PT_CONTINUE, pid, 1, 17);
        while (wait4(-1, &status, WUNTRACED, NULL) != pid);
        show (status);
        */
        if (ptrace (PT_DETACH, pid, 1, 0))
                err (1, "ptrace detach");
        else
                printf ("detach ok\n");
        exit (1);
}

Reply via email to