问题描述
我使用ptrace的跟踪子进程。它的工作原理非常清楚,当子进程正常退出。但是,如果它退出异常,程序进入在尽管使用宏WIFSIGNALED(安培;状态)的无限循环。下面是示例子进程:
I am using ptrace to trace a child process. It works perfectly well when the child process exit normally. But if it exit abnormally, the program get into an infinite loop in-spite of using the macro WIFSIGNALED(&status). Here is sample child process:
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
Why the signal is not being detected which otherwise works when ptrace is not used?
推荐答案
在ptrace的一个过程,等待返回任何状态变化。其中之一是当处理即将接收信号。你的等待将返回的前的信号被传递到子。您需要使用PTRACE_CONT允许信号被传递给孩子,如果这就是你要发生什么。
When you ptrace a process, wait will return for any state change. One of those is when the process is about to receive a signal. Your wait will return before the signal is delivered to the child. You need to use PTRACE_CONT to allow the signal to be delivered to the child, if that's what you want to happen.
为什么它这样工作?清楚地记得,ptrace的主要目的是在实施调试器使用。如果你没有得到一个机会来截取信号,如 SIGSEGV
,调试器不能停下来让你检查赛格故障的过程中被拆掉了。
Why does it work this way? Well remember, ptrace's main purpose is to be used in implementing debuggers. If you didn't get a chance to intercept signals such as SIGSEGV
, the debugger couldn't stop and let you examine the seg fault before the process was torn down.
这篇关于为什么WIFSIGNALED(状态)无法检测到信号,同时跟踪与ptrace的一个过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!