GDB调试fork+exec创建的子进程的方法

您所在的位置:网站首页 gdb调试dlopen函数 GDB调试fork+exec创建的子进程的方法

GDB调试fork+exec创建的子进程的方法

2023-12-16 12:36| 来源: 网络整理| 查看: 265

 

 

[root@centos7 ~]# cat test.c #include #include #include int main() { int ret = 0; ret = fork(); if (ret == 0) { execv("child", NULL); //child.c编译成的可执行文件 } return 0; } [root@centos7 ~]# cat child.c #include #include #include int my_print(); int main() { my_print(); return 0; } int my_print() { printf("hello world\n"); return 0; } [root@centos7 ~]#

 

 

[root@centos7 ~]# gcc -g test.c -o test [root@centos7 ~]# gcc -g child.c -o child [root@centos7 ~]# gdb test GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7 Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "aarch64-redhat-linux-gnu". For bug reporting instructions, please see: ... Reading symbols from /root/test...done. (gdb) set follow-fork-mode child (gdb) catch exec Catchpoint 1 (exec) (gdb) r Starting program: /root/test [Attaching after process 125192 fork to child process 125192] [New inferior 2 (process 125192)] [Detaching after fork from parent process 125189] [Inferior 1 (process 125189) detached] process 125192 is executing new program: /root/child Missing separate debuginfos, use: debuginfo-install glibc-2.17-324.el7_9.aarch64 [Switching to process 125192] Catchpoint 1 (exec'd /root/child), 0x0000ffffbe7d10e0 in _start () from /lib/ld-linux-aarch64.so.1 Missing separate debuginfos, use: debuginfo-install glibc-2.17-324.el7_9.aarch64 (gdb) inferior Argument required (expression to compute). (gdb) inferiors Undefined command: "inferiors". Try "help". (gdb) b main Breakpoint 2 at 0x400608: file child.c, line 8. (gdb) list 1 #include 2 #include 3 #include 4 5 int my_print(); 6 int main() 7 { 8 my_print(); 9 return 0; 10 } (gdb) b my_print Breakpoint 3 at 0x400620: file child.c, line 14. (gdb) c Continuing. Breakpoint 2, main () at child.c:8 8 my_print(); (gdb) list 3 #include 4 5 int my_print(); 6 int main() 7 { 8 my_print(); 9 return 0; 10 } 11 12 int my_print() (gdb) s Breakpoint 3, my_print () at child.c:14 14 printf("hello world\n"); (gdb) n hello world 15 return 0; (gdb)

 

上面的例子中,最重要的操作时catch exec这个事件。捕获到exec这个事件之后再往子进程的程序中打一个断点,然后执行continue操作。可以看到,此时程序就会进入到exec调用的子进程中了。

[root@centos7 ~]# ps -elf | grep test 0 S root 125012 121326 0 80 0 - 2219 poll_s 05:51 pts/0 00:00:00 gdb test 0 S root 126358 126176 0 80 0 - 1730 pipe_w 05:56 pts/1 00:00:00 grep --color=auto test [root@centos7 ~]# ps -elf | grep child 0 t root 125192 1 0 80 0 - 38 ptrace 05:52 pts/0 00:00:00 [child] 0 S root 126382 126176 0 80 0 - 1730 pipe_w 05:56 pts/1 00:00:00 grep --color=auto child [root@centos7 ~]#

更改test

[root@centos7 ~]# cat test.c #include #include #include int main() { int ret = 0; ret = fork(); if (ret == 0) { execv("child", NULL); //child.c编译成的可执行文件 } printf("main over \n"); return 0; }

 

 

[root@centos7 ~]# gcc -g test.c -o test[root@centos7 ~]# gdb testGNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7Copyright (C) 2013 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later This is free software: you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law. Type "show copying"and "show warranty" for details.This GDB was configured as "aarch64-redhat-linux-gnu".For bug reporting instructions, please see:...Reading symbols from /root/test...done.(gdb) catch exec Catchpoint 1 (exec)(gdb) set follow-fork-mode child(gdb) cThe program is not being run.(gdb) rStarting program: /root/test [Attaching after process 126997 fork to child process 126997][New inferior 2 (process 126997)][Detaching after fork from parent process 126994][Inferior 1 (process 126994) detached]main over  --- main结束了process 126997 is executing new program: /root/childMissing separate debuginfos, use: debuginfo-install glibc-2.17-324.el7_9.aarch64[Switching to process 126997]

Catchpoint 1 (exec'd /root/child), 0x0000ffffbe7d10e0 in _start () from /lib/ld-linux-aarch64.so.1Missing separate debuginfos, use: debuginfo-install glibc-2.17-324.el7_9.aarch64(gdb)

 

[root@centos7 ~]# ps -elf | grep 12699 0 t root 126997 1 0 80 0 - 12 ptrace 05:59 pts/0 00:00:00 [child] 0 S root 127440 126176 0 80 0 - 1730 pipe_w 06:00 pts/1 00:00:00 grep --color=auto 12699 [root@centos7 ~]#

 

测试2

[root@centos7 ~]# cat main.c #include #include int main(int argc,char* argv[]) { char *my[3] = {0}; my[0] = "nihao"; my[1] = "-l"; int ret = execv("child",my); printf("ret = %d", ret); return 0; } [root@centos7 ~]# cat child.c #include #include #include int main() { int * p =NULL; *p = 3; return 0; } [root@centos7 ~]#

 

 

 

[root@centos7 ~]# gcc -g main.c -o main [root@centos7 ~]# ./main 段错误 [root@centos7 ~]# gdb main GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7 Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "aarch64-redhat-linux-gnu". For bug reporting instructions, please see: ... Reading symbols from /root/main...done. (gdb) r Starting program: /root/main process 129214 is executing new program: /root/child Missing separate debuginfos, use: debuginfo-install glibc-2.17-324.el7_9.aarch64 Program received signal SIGSEGV, Segmentation fault. 0x00000000004005c0 in main () at child.c:8 8 *p = 3; Missing separate debuginfos, use: debuginfo-install glibc-2.17-324.el7_9.aarch64 (gdb) bt #0 0x00000000004005c0 in main () at child.c:8 (gdb)

 

执行一个非二进制函数

[root@centos7 ~]# gcc -g main.c -o main [root@centos7 ~]# ./main ret = -1 [root@centos7 ~]# cat main.c #include #include int main(int argc,char* argv[]) { char *my[3] = {0}; my[0] = "nihao"; my[1] = "-l"; int ret = execv("child.c",my); printf("ret = %d \n ", ret); return 0; } [root@centos7 ~]#

执行一个不存在的

[root@centos7 ~]# gcc -g main.c -o main [root@centos7 ~]# ./main ret = -1 [root@centos7 ~]#cat main.c #include #include int main(int argc,char* argv[]) { char *my[3] = {0}; my[0] = "nihao"; my[1] = "-l"; int ret = execv("child.ccc",my); printf("ret = %d \n ", ret); return 0; } [root@centos7 ~]#

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3