执行调用后的gdb调试过程

编程入门 行业动态 更新时间:2024-10-23 16:16:12
本文介绍了执行调用后的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调试过程

本文发布于:2023-10-31 07:27:20,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:过程   gdb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!