我正在为我的大学班级构建一个简单的调试器,我在处理SIGINT时遇到了一个问题。
我想要做的是,当调试器进程(从现在开始PDB)接受一个SIGINT信号,将其传递给子进程(PDB实际正在调试的那个进程)。
我正在这样做:
pid_t childid;
void catch_sigint(int sig)
{
signal(SIGINT,SIG_DFL);
kill(childid,sig);
}
int debuger (char *address, parm *vars)
{
int ignore=1;
int status;
childid = fork();
signal(SIGINT,catch_sigint);
if(childid==0)
{
ptrace(PTRACE_TRACEME,0, NULL,NULL);
if(execve(address,NULL,NULL)==-1)
{
perror("ERROR occured when trying to create program to trace\n");
exit(1);
}
}
else
{
int f_time=1;
while(1)
{
long system_call;
wait(&status);
if(WIFEXITED(status))break;
if(WIFSIGNALED(status))break;
system_call = ptrace(PTRACE_PEEKUSER,childid, 4 * ORIG_EAX, NULL);
if(!strcmp(vars->category,"process-control") || !strcmp(vars->category,"all"))
ignore = pr_calls(system_call,ignore,limit,childid,vars->mode); //function that takes the system call that is made and prints info about it
if(!strcmp(vars->category,"file-management") || !strcmp(vars->category,"all"))
ignore = fl_calls(system_call,ignore,limit,childid,vars->mode);
if(f_time){ignore=1;f_time=0;}
ptrace(PTRACE_SYSCALL,childid, NULL, NULL);
}
}
signal(SIGINT,SIG_DFL);
return 0;
}
这个程序运行和派生一个子进程,并执行一个程序来跟踪它的系统调用。当它没有接收到任何信号时,它工作得很好。
但是,当我在一些跟踪过程中按下ctrl+c时,我希望子进程停止,PDB继续并停止(因为这一行是if(WIFSIGNALED(status))break;
。这永远不会发生。它跟踪的程序继续它的系统调用和打印。
跟踪程序是:
#include <stdio.h>
int main(void)
{
for(;;) printf("HELLO WORLD\n");
return 0;
}
即使在我点击ctrl+c之后,该程序仍会继续打印HELLO WORLD。
我还观察到,ptrace在ctrl+c之后给出的系统调用是-38,并且在信号从1407 (我认为是正常值)到639,然后在下一次等待时又回到1407之后,等待中的状态只更改一次。
那么我到底做错了什么呢?
发布于 2013-04-21 04:09:55
问题在这一行上:
ptrace(PTRACE_SYSCALL,childid, NULL, NULL);
必须是这样的:
ptrace(PTRACE_SYSCALL,childid, NULL, signal_variable);
其中signal_variable
是在全局作用域中声明的整数,以便处理程序和调试器可以看到它。它的起始值为0。
信号处理程序现在接受信号并将其传递到这个变量中,在下一个循环中,当ptrace命令tracee程序继续时,它也会向它发送信号。这是因为当您跟踪程序时,tracee会在收到信号时停止执行,并等待进一步的指令,以便通过ptrace处理来自跟踪器的信号。
https://stackoverflow.com/questions/16120871
复制相似问题