【发布时间】:2016-07-04 12:59:04
【问题描述】:
我正在尝试在我自己的进程中监控/重定向系统调用。当 fwrite 在 libc 中调用 write 时,LD_PRELOAD 不起作用,而 got/plt 钩子似乎也有同样的问题。我正在寻找基于 ptrace 的解决方案,但我不能 fork() 并将主应用程序作为子应用程序运行,因为该应用程序通过信号与其父应用程序通信。
有一个 2006 年的线程表明跟踪器可以位于与被跟踪者不同的线程组上,但在实践中似乎不起作用:http://yarchive.net/comp/linux/ptrace_self_attach.html
pid = fork();
if (pid == 0) {
prctl(PR_SET_PTRACER, getppid());
raise(SIGSTOP);
} else {
sleep(1);
ptrace(PTRACE_SEIZE, pid, NULL, NULL);
for (;;) {
int status;
int ret = waitpid(pid, &status, 0);
warn("wait=%d:", ret);
ret = ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
warn("ptrace=%d:", ret);
}
}
我面临的问题是 ptrace(PTRACE_SYSCALL) 期望被跟踪者处于 ptrace-wait 状态,即它必须引发 SIGSTOP 并且跟踪器需要为其等待()。由于在这种情况下关系是相反的(tracer 是 tracee 的子节点)PTRACE_SYSCALL 返回 ESRCH。
strace 如何摆脱跟踪现有 pid 的问题?
【问题讨论】:
标签: linux fork posix strace ptrace