gdb调试包含popen的程序,程序停在popen中,导致gdb假死状态,无法退出

新手上路,请多包涵

1、程序中包含popen()调用,在使用gdb调试时,gdb假死,定位到程序运行到popen
2、编写测试程序验证,确实如此,程序如下:
int main(int argc, char *argv[])
{

    char buf[128];
    FILE *pp;

    printf("%s,%d\n", __FILE__, __LINE__);
    if( (pp = popen("ls -l", "r")) == NULL ){
            printf("popen() error!/n");
            exit(1);
    }
    printf("%s,%d\n", __FILE__, __LINE__);
    while(fgets(buf, sizeof buf, pp)){
            printf("%s", buf);
    }
    printf("%s,%d\n", __FILE__, __LINE__);
    pclose(pp);
    printf("%s,%d\n", __FILE__, __LINE__);
    return 0;

}
3、gdb日志
GNU gdb (GDB) 7.10.1
Copyright (C) 2015 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 "arm-openwrt-linux".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
http://www.gnu.org/software/g....
Find the GDB manual and other documentation resources online at:
http://www.gnu.org/software/g....
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /mnt/nfs/work01/src2git/test-share/main...done.
(gdb) r
Starting program: /mnt/nfs/work01/src2git/test-share/main
warning: Unable to find dynamic linker breakpoint function.
GDB will be unable to debug shared library initializers
and track explicitly loaded dynamic code.
main.c,92

^C^C^C^C
程序停在popen语句,ctrl+c无法退出。
4、google时,有人提出是popen fork进程导致GDB混乱,未给出好的解决办法(不使用popen)
https://daniel.haxx.se/blog/2...

是否有高手可以解决这个问题,或者有其他方法调用shell命令并获得返回值

阅读 3.8k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题