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


    [root@centos7 ~]# cat test.c 
    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
     
    int main()
    {
        int ret = 0;
        ret = fork();
        if (ret == 0) {
             execv("child", NULL);  //child.c编译成的可执行文件
        }
     
        return 0;
    }
    [root@centos7 ~]# cat child.c 
    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
     
    int my_print();
    int main()
    {
        my_print();
        return 0;
    }
     
    int my_print()
    {
        printf("hello world
    ");
        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 <http://gnu.org/licenses/gpl.html>
    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:
    <http://www.gnu.org/software/gdb/bugs/>...
    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 <stdlib.h>
    2       #include <stdio.h>
    3       #include <unistd.h>
    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 <unistd.h>
    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
    ");
    (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 <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
     
    int main()
    {
        int ret = 0;
        ret = fork();
        if (ret == 0) {
             execv("child", NULL);  //child.c编译成的可执行文件
        }
        printf("main over 
    "); 
        return 0;
    }

    [root@centos7 ~]# gcc -g test.c -o test
    [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 <http://gnu.org/licenses/gpl.html>
    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:
    <http://www.gnu.org/software/gdb/bugs/>...
    Reading symbols from /root/test...done.
    (gdb) catch exec
    Catchpoint 1 (exec)
    (gdb) set follow-fork-mode child
    (gdb) c
    The program is not being run.
    (gdb) r
    Starting 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/child
    Missing 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.1
    Missing 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 <unistd.h>
    #include <stdio.h>
    
    
    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 <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
     
    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 <http://gnu.org/licenses/gpl.html>
    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:
    <http://www.gnu.org/software/gdb/bugs/>...
    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 <unistd.h>
    #include <stdio.h>
    
    
    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 
     ", ret);    
      return 0;
    }
    
    [root@centos7 ~]# 

    执行一个不存在的

    [root@centos7 ~]# gcc -g   main.c -o main
    [root@centos7 ~]# ./main 
    ret = -1 
     [root@centos7 ~]#cat main.c 
    #include <unistd.h>
    #include <stdio.h>
    
    
    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 
     ", ret);    
      return 0;
    }
    
    [root@centos7 ~]# 
  • 相关阅读:
    升级centos6.5系统的gcc为4.8.5的简易步骤
    赛车比赛(洛谷U4566)
    月考(cogs 1176)
    xth的旅行(codevs 1450)
    魔法禁书目录2:回家(codevs 3024)
    交换
    牛的旅行(洛谷 1522)
    长途旅行
    序列问题
    正确答案
  • 原文地址:https://www.cnblogs.com/dream397/p/15449194.html
Copyright © 2020-2023  润新知