开发者社区 问答 正文

fork后直接执行语句不能被ptrace跟踪,而调用execve后可以是什么原因。

请问这是什么情况

pid_t pid = fork();
if (pid == 0) {
    if(ptrace(PTRACE_TRACEME, 0, NULL, NULL) == -1) {
        printf("error in child: %s\n", strerror(errno));
        exit(2);
    }
    write(2, "hello\n", 6);  //情况A
    //execlp("/home/bman/HelloWolrd", ""); //情况B
} else {
    while(1) {
        int status = 0;
        wait(&status);
        if (WIFEXITED(status)) {
            break;
        }
        user_regs_struct reg;
        int ret = ptrace(PTRACE_GETREGS, pid, NULL, &reg );
        if (ret == -1) {
            printf("%s\n", strerror(errno));
            exit(2);
        }
        printf("ret %d\n", ret);
        printf("syscall %d\n", (int)reg.orig_eax);
        getchar();
        ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
    }
}

情况A的子进程直接输出hello后退出

展开
收起
a123456678 2016-06-24 14:41:43 2018 分享 版权
1 条回答
写回答
取消 提交回答
  • pid_t pid = fork();
    if (pid == 0) {
        if(ptrace(PTRACE_TRACEME, 0, NULL, NULL) == -1) {
            printf("error in child: %s\n", strerror(errno));
            exit(2);
        }
        write(2, "hello\n", 6);  //情况A
        //execlp("/home/bman/HelloWolrd", ""); //情况B
    } else {
        while(1) {
            int status = 0;
            wait(&status);
            if (WIFEXITED(status)) {
                break;
            }
            user_regs_struct reg;
            int ret = ptrace(PTRACE_GETREGS, pid, NULL, &reg );
            if (ret == -1) {
                printf("%s\n", strerror(errno));
                exit(2);
            }
            printf("ret %d\n", ret);
            printf("syscall %d\n", (int)reg.orig_eax);
            getchar();
            ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
        }
    }
    2019-07-17 19:46:57
    赞同 展开评论
问答地址: