执行调用后的gdb调试过程 [英] gdb debugging process after exec call

查看:170
本文介绍了执行调用后的gdb调试过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调用 execle 之后,我不知道如何调试。我查看了其他网站,并建议使用 set fork-follow-mode child ,这有助于我进入分支。但是,在fork之后,我退出到主函数中,从不进入我正在执行的程序。



以下是代码:

 } else if(!( pid_T2 = fork())){
char ** env = NULL;
char * units_env = NULL;
char * sleep_env = NULL;
size_t sleep_sz = 16;

env =(char **)malloc(3 * sizeof(char *));
sleep_env =(char *)malloc(sleep_sz * sizeof(char));
snprintf(sleep_env,sleep_sz,TSTALL =%d,cmd_args-> sleep_num);
if(cmd_args-> kb){
units_env =UNITS = 1;
} else {
units_env =UNITS = 0;
}
*(env)= units_env; *(env + 1)= sleep_env; *(env + 2)=TMOM = 0;
$ b / * printf(%s%s\\\
,*(env),*(env + 1)); * /

close(pipe_A2toT2 [1 ]);
dup2(pipe_A2toT2 [0],0);
close(pipe_A2toT2 [0]);

execle(totalsize,totalsize,NULL,env); //在此行后退出,不会进入程序。
}

我知道过程映像被exec调用替换,但为什么我仍然退出到这个程序的主体,而不是进入 totalsize 程序?

解决方案


以下是代码:

这不是 代码。这是代码中不可编译和无意义的代码片段。
您还没有告诉您正在使用哪个操作系统,或者您使用了哪些GDB命令。



以下示例显示了这是如何假设的在Linux上工作:

// echo.c

  #include< stdio.h> 

int main(int argc,char * argv [0]){
for(int i = 1; i< argc; ++ i){
if(i != 1)printf();
printf(%s,argv [i]);
}
printf(\\\
);
返回0;
}

// exec.c

  #include< stdio.h> 
#include< stdlib.h>
#include< sys / types.h>
#include< sys / wait.h>
#include< unistd.h>

int main()
{
pid_t pid = fork();
int状态;

if(pid == 0){
execlp(./ echo,echo,aa,bb,(char *)0);
abort();
} else {
printf(父%d等待%d\\\
,getpid(),pid);
waitpid(pid,& status,0);
printf(child%d exited%d \\\
,pid,status);
}
返回状态;

$ / code>

现在让我们编译这些并在GDB下运行:

  gcc -g -std = c99 -o echo echo.c 
gcc -g -o exec exec.c

gdb -q ./exec
读取./exec...done中的符号。
(gdb)设置后续分支模式子
(gdb)break main
断点1在0x4006a5处:文件exec.c,第9行。
(gdb)run
启动程序:/ tmp / exec

断点1,main()在exec.c:9
9 pid_t pid = fork();
(gdb)c
继续。
[新进程9851]

注意上面的GDB附加了新程序,因为 follow-fork-mode 告诉它。

 父母9832等待9851 
进程9851正在执行新程序:/ tmp / echo

请注意GDB如何注意到进程是执行一个新的二进制文件。

pre code $ [$切换到进程9851]

断点1,main(argc = (int i = 1; i< argc; ++ i){

请注意,我们现在停止在不同的 主要中。


I don't know how to debug after the process after calling execle. I've looked at other websites and some suggested using set fork-follow-mode child, which helped me get into the fork. However, after the fork, I exit into the main function and never get into the program I am exec'ing.

Here is the code:

            } else if (!(pid_T2 = fork())) {
                char **env = NULL;
                char *units_env = NULL;
                char *sleep_env = NULL;
                size_t sleep_sz = 16;

                env = (char **) malloc(3 * sizeof(char *));
                sleep_env = (char *) malloc(sleep_sz * sizeof(char));
                snprintf(sleep_env, sleep_sz, "TSTALL=%d", cmd_args->sleep_num);
                if (cmd_args->kb) {
                        units_env = "UNITS=1";
                } else {
                        units_env = "UNITS=0";
                }
                *(env) = units_env; *(env + 1) = sleep_env; *(env + 2) = "TMOM=0";

                /*printf("%s %s\n", *(env), *(env + 1));*/

                close(pipe_A2toT2[1]);
                dup2(pipe_A2toT2[0], 0);
                close(pipe_A2toT2[0]);

                execle("totalsize", "totalsize", NULL, env); //Exits to main after this line, never goes into program.
          }

I know that the process image gets replaced by exec call, however why am I still exiting to this program's main instead of going into totalsize program?

解决方案

Here is the code:

That's not the code. That's an un-compilable and meaningless snippet of the code. You also didn't tell what OS you are using, or which GDB commands you used.

Here is an example showing how this is supposed to work, on Linux:

// echo.c

#include <stdio.h>

int main(int argc, char *argv[0]) {
  for (int i = 1; i < argc; ++i) {
    if (i != 1) printf(" ");
    printf("%s", argv[i]);
  }
  printf("\n");
  return 0;
}

// exec.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main()
{
  pid_t pid = fork();
  int status;

  if (pid == 0) {
    execlp("./echo", "echo", "aa", "bb", (char*)0);
    abort();
  } else {
    printf("parent %d waiting for %d\n", getpid(), pid);
    waitpid(pid, &status, 0);
    printf("child %d exited %d\n", pid, status);
  }
  return status;
}

Now let's compile this all and run under GDB:

gcc -g -std=c99 -o echo echo.c
gcc -g -o exec exec.c

gdb -q ./exec
Reading symbols from ./exec...done.
(gdb) set follow-fork-mode child
(gdb) break main
Breakpoint 1 at 0x4006a5: file exec.c, line 9.
(gdb) run
Starting program: /tmp/exec 

Breakpoint 1, main () at exec.c:9
9         pid_t pid = fork();
(gdb) c
Continuing.
[New process 9851]

Note how GDB attached new program above, because follow-fork-mode told it to.

parent 9832 waiting for 9851
process 9851 is executing new program: /tmp/echo

Note how GDB noticed that the process is executing a new binary.

[Switching to process 9851]

Breakpoint 1, main (argc=3, argv=0x7fffffffe8d8) at echo.c:4
4         for (int i = 1; i < argc; ++i) {

Note that we are now stopped in a different main.

这篇关于执行调用后的gdb调试过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆