gpt4 book ai didi

c - 为什么 WIFSIGNALED(status) 在使用 ptrace 跟踪进程时无法检测到信号?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:00:10 26 4
gpt4 key购买 nike

我正在使用 ptrace 来跟踪子进程。当子进程正常退出时,它工作得很好。但是,如果它异常退出,尽管使用了宏 WIFSIGNALED(&status),程序仍会进入死循环。这是示例子进程:

try.c

int main()
{
int a=5/0;
}

这是跟踪程序

#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/user.h>
#include <sys/syscall.h> /* For SYS_write etc */
#include <sys/reg.h>
#include <signal.h>
int main()
{
pid_t child;
long orig_eax, eax;
int status,insyscall = 0;
child = fork();
if(child == 0)
{
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
execl("./try", "try", NULL);
}
else
{
siginfo_t sig;
memset(&sig,0,sizeof(siginfo_t));
while(1)
{
wait(&status);
if(WIFSIGNALED(status))
{
printf("Exiting due to signal\n");
exit(0);
}
if(WIFEXITED(status))
break;
orig_eax = ptrace(PTRACE_PEEKUSER,child, 4 * ORIG_EAX, NULL);
printf("system call number=%ld\n",orig_eax);
if(insyscall == 0)
{
/* Syscall entry */
insyscall = 1;
printf("In sys call\n");
}
else
{
/* Syscall exit */
eax = ptrace(PTRACE_PEEKUSER,child, 4 * EAX, NULL);
printf("System call returned with %ld\n", eax);
insyscall = 0;
}
ptrace(PTRACE_SYSCALL,child, NULL, NULL);
}
}
return 0;
}

为什么没有检测到信号,否则在不使用 ptrace 时可以正常工作?

最佳答案

当你 ptrace 一个进程时,wait 会返回任何状态变化。其中之一是进程即将接收信号时。您的等待将在信号传递给 child 之前返回。你需要使用 PTRACE_CONT 来允许将信号传递给 child ,如果这是你想要发生的。

为什么会这样?请记住,ptrace 的主要目的是用于实现调试器。如果您没有机会拦截诸如 SIGSEGV 之类的信号,调试器将无法停止并让您在进程被拆除之前检查段错误。

关于c - 为什么 WIFSIGNALED(status) 在使用 ptrace 跟踪进程时无法检测到信号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7462230/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com