2011-04-24 143 views
1

我的项目是通过结束与&的arglist方法来实现一个带有后台处理的简单shell程序,就像在大多数UNIX shell中一样。我的问题是如何在后台处理需要创建子进程时在GDB中调试shell。调试子进程 - GDB/DDD

我的孩子处理代码是这样

int id; 
int child=-1; 
int running=0; 

if ((strcmp(args[0], "&")==0){ 

    if ((id==fork())==-1) 
    perror("Couldn't start the background process"); 

    else if (id==0){ //start the child process 
    running++; 
    printf("Job %d started, PID: %d\n", running, getpid()); 
    signal(SIGINT, SIG_DFL); 
    signal(SIGQUIT, SIG_DFL); 
    execvp(args[0], args); 
    perror("Can't execute command); 
    exit(1); 

    else { 
    int jobNum= running-(running-1); 

    if ((waitpid(-1, &child, WNOHANG) == -1) 
    perror("Child Wait"); 

    else 
    printf("[%d] exited with status %d\n", jobNum, child>>8); 
} 

当我试图运行一个命令,如ps &,并设置断点功能分析器,执行该命令没有击中断点。这是令人困惑的,并使调试器在这种情况下无用。我能做些什么呢?

回答

1

我想你想

set follow-fork-mode child 

也注意到,线

if ((id==fork())==-1) 

是对叉的返回值进行比较未初始化值()。 我相信你想要一个任务。

+0

第二个是我的错误,但第一个是什么意思? – Jason 2011-04-24 01:38:54

+0

@jason gdb命令'set follow-fork-mode'告诉gdb在遇到fork时应该怎么做,fork的哪一边应该继续调试,父节点还是子节点。 – matt 2011-04-24 01:42:45

+0

感谢您的澄清。 – Jason 2011-04-24 01:43:47