我正在尝试监视子进程是否存在段错误错误,但这不起作用。
我总是收到 ABRT 信号。
我看到 gdb 可以捕获段错误,那么我的代码有什么问题?
pid_t child;
int wstatus, signum;
struct user_regs_struct regs;
child = fork();
if (child == 0)
{
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
char buf[10];
// make it always crash
strcpy (buf, "aaaaaaabbbbbbbbbbbaaaaaaaaaaaaaaaa");
printf ("Buf is %s\n", buf);
exit(0);
}
while(1)
{
wait(&wstatus);
if (WIFEXITED(wstatus) || WIFSIGNALED(wstatus))
break;
signum = WSTOPSIG(wstatus);
ptrace(PTRACE_GETREGS, child, NULL, ®s);
printf ("signal: %d, eip: 0x%08lx\n", signum, regs.eip);
ptrace(PTRACE_CONT, child, NULL, signum);
}