为什么SIGINT被发送到子进程并且什么都不做?

时间:2013-04-20 13:23:14

标签: c unix signals posix ptrace

我正在为我的大学课程构建一个简单的调试器,我在处理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的信号之后仅改变一次在下一次等待。

那我在做错了什么?

1 个答案:

答案 0 :(得分:0)

问题在于这一行:

ptrace(PTRACE_SYSCALL,childid, NULL, NULL);

必须是这样的:

ptrace(PTRACE_SYSCALL,childid, NULL, signal_variable);

其中signal_variable是在全局范围内声明的int,因此处理程序和调试器可以看到它。它的起始值为0.

信号处理程序现在接收信号并将其传递给此变量,并在下一个循环中,当ptrace命令tracee程序继续时,它也会发送信号。 发生这种情况是因为当您跟踪程序时,tracee会在收到信号时停止执行,并通过ptrace等待有关如何处理来自跟踪器的信号的进一步说明。

相关问题