小编kar*_*kki的帖子

在PTRACE_CONT之后PTRACE_DETACH失败并且errno = ESRCH

在我的项目中,我需要附加到进程,恢复它们,然后再使用ptrace.但是,分离失败了errno=ESRCH (No such process).

如果我没有恢复进程,则分离工作正常PTRACE_CONT,但在这种情况下,进程停止/无响应,这在我的项目中是不可接受的.在Arch和Ubuntu 12.04 LTS上测试,结果相同.

#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;
}
Run Code Online (Sandbox Code Playgroud)

输出:

attached …
Run Code Online (Sandbox Code Playgroud)

c linux ptrace

4
推荐指数
1
解决办法
2323
查看次数

标签 统计

c ×1

linux ×1

ptrace ×1