2013-04-20 33 views
1

我正在爲我的大學課堂構建一個簡單的調試器,並且在處理SIGINT時遇到了問題。爲什麼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; 
} 

的運行程序和fork一個子進程和exec一個程序來跟蹤它的系統調用。當它沒有得到任何信號時,它工作正常。

但是當在一些跟蹤的中間我按ctrl + c我期望子進程停止,並且PDB繼續並停止(因爲這條線if(WIFSIGNALED(status))break;。這從來沒有發生過。它跟蹤它的程序繼續它的系統調用。和打印

的追蹤程序是:

#include <stdio.h> 

int main(void) 
{ 
    for(;;) printf("HELLO WORLD\n");   
    return 0; 
} 

該方案繼續打印HELLO WORLD我打了CTRL + C即使

我還觀察到,系統調用的ptrace℃之後給出。 trl + c是-38,並且在等待狀態從1407(我認爲是正常值)到639的信號只改變一次,然後在下一次等待時再次回到1407。

那麼我在做什麼錯?

回答

0

這是該行的問題:

ptrace(PTRACE_SYSCALL,childid, NULL, NULL); 

它必須是這樣的:

ptrace(PTRACE_SYSCALL,childid, NULL, signal_variable); 

signal_variable是在全球範圍內宣佈這樣的處理程序和調試器可以看到它的int 。它的起始值爲0.

信號處理程序現在接收信號並將其傳遞到此變量中,並在下一個循環時,當ptrace命令tracee程序繼續向它發送信號時。 發生這種情況的原因是,當你跟蹤一個程序時,tracee在它接收到一個信號時停止執行,並等待進一步的指令來處理通過ptrace來自跟蹤器的信號。