1

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

我的孩子处理代码就像

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 &)并将断点设置为函数解析器时,该命令执行时不会遇到断点。这很令人困惑,并且在这种情况下会使调试器无用。我能做些什么呢?

4

1 回答 1

1

我想你想要

set follow-fork-mode child

另请注意,该行

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

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

于 2011-04-24T01:35:14.790 回答