【发布时间】:2013-12-14 04:24:47
【问题描述】:
当我附加到另一个进程时,我无法跟踪 fork / exec 事件,从 waitpid 返回的 status 始终为零(右移 16 次后)。
我已成功附加到 bash shell,但无论我运行什么命令,状态始终为零,因此我没有捕获任何 fork 或 exec 事件:
#define PALL PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE \
| PTRACE_O_TRACEEXEC | PTRACE_O_TRACEVFORKDONE | PTRACE_O_TRACEEXIT
int main(int argc, char **argv)
{
pid_t child = 0;
int status = 0;
if (argc != 2) { ... }
child = atoi (argv[1]);
if (ptrace (PTRACE_ATTACH, child, 0, 0) < 0) { ... }
ptrace(PTRACE_SETOPTIONS, child, NULL, PALL);
ptrace(PTRACE_SYSCALL, child, NULL, NULL);
ptrace(PTRACE_CONT, child, NULL, NULL);
while(1) {
waitpid(child, &status, 0);
if(WIFEXITED(status))
break;
status >>= 16;
if (status != 0)
printf ("Status: %d\n", status >> 16);
ptrace(PTRACE_SYSCALL, child, NULL, NULL);
}
ptrace(PTRACE_DETACH, child, NULL, NULL);
return 0;
}
【问题讨论】: