gdb attach一个子进程后,ctrl+c为何无法退出?

水木山 2012-04-18 03:00:31
代码如下:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

void test(pid_t pid)
{
while(1) {
sleep(1);
}
}

void test1(pid_t pid)
{
while(1) {
sleep(1);
}
}


int main()
{
pid_t pid = 0;
int ret = 0;

pid = fork();
if (pid == 0) {
//setsid();
test1(pid);
} else if (pid > 0)
{
printf("This is the parent process! my id=%d ,child process id = %d\n", getpid(),pid);
test(pid);
} else {
printf("fork error\n");
}

return 1;
}

gdb对子进程进行调试:
aven@dolphin-j:~> ./ctrl_c_test &
[1] 6537
This is the parent process! my id=6537 ,child process id = 6538
aven@dolphin-j:~> gdb attach 6538
GNU gdb (GDB) SUSE (7.0-0.4.16)
Copyright (C) 2009 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 "x86_64-suse-linux".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
attach: No such file or directory.
Attaching to process 6538
Reading symbols from /home/aven/ctrl_c_test...done.
Reading symbols from /lib64/libc.so.6...Missing separate debuginfo for /lib64/libc.so.6
Try: zypper install -C "debuginfo(build-id)=a41ac0b0b7cd60bd57473303c2c3de08856d2e06"
(no debugging symbols found)...done.
Loaded symbols for /lib64/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...Missing separate debuginfo for /lib64/ld-linux-x86-64.so.2
Try: zypper install -C "debuginfo(build-id)=17c088070352d83e7afc43d83756b00899fd37f0"
(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
0x00007f9b618bf060 in __nanosleep_nocancel () from /lib64/libc.so.6
(gdb) c
Continuing.
^C^C^C^C^Z

gdb attach父进程则能够正常退出。ctrl+c只能用于前台的进程,我不知道此处是否是因为gdb attach的时候不能把子进程拿到前台来运行。

另外,在子进程中用setsid()切断和现在进程组(作业)的关系,或者新开一个session(窗口)来gdb调试子进程,都能够正常退出。什么原因?

请大神赐教~~
...全文
1974 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
水木山 2012-04-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
我使用ubuntu调试是问题可以重现。
在同一终端中启动进程时,在去调试子进程是父进程在这个终端中不能捕获SIGINT这个信号,子进程就无法终止。如果在另外的终端去调试子进程时,父进程可以捕获到这个信号就可以使子进程停止。
icense GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is ……
[/Quote]
是啊,新开一个终端或者在子进程中加入setsid()都是可以的,就是不知道具体的原因,为什么在原来的终端下不行。
shiguangjiqi 2012-04-18
  • 打赏
  • 举报
回复
我使用ubuntu调试是问题可以重现。
在同一终端中启动进程时,在去调试子进程是父进程在这个终端中不能捕获SIGINT这个信号,子进程就无法终止。如果在另外的终端去调试子进程时,父进程可以捕获到这个信号就可以使子进程停止。
icense 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 "i686-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
(gdb) attach 13659
Attaching to process 13659
Reading symbols from /home/dongbin/student/c++/2012/app...done.
Reading symbols from /usr/lib/i386-linux-gnu/libstdc++.so.6...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libstdc++.so.6
Reading symbols from /lib/i386-linux-gnu/libm.so.6...Reading symbols from /usr/lib/debug/lib/i386-linux-gnu/libm-2.13.so...done.
done.
Loaded symbols for /lib/i386-linux-gnu/libm.so.6
Reading symbols from /lib/i386-linux-gnu/libgcc_s.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib/i386-linux-gnu/libgcc_s.so.1
Reading symbols from /lib/i386-linux-gnu/libc.so.6...Reading symbols from /usr/lib/debug/lib/i386-linux-gnu/libc-2.13.so...done.
done.
Loaded symbols for /lib/i386-linux-gnu/libc.so.6
Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/ld-linux.so.2
0x00cf1416 in __kernel_vsyscall ()
(gdb) c
Continuing.
^C
Program received signal SIGINT, Interrupt.
0x00cf1416 in __kernel_vsyscall ()
(gdb)
jsy5211 2012-04-18
  • 打赏
  • 举报
回复
我用的是cygwin
好像弄不出来:
info thread 有3个 1440是父进程
3 thread 1440.XX
2 thread 1440.XX
1 thread 1440.XX

但是我在单位是可以的 不知道你的是啥环境
水木山 2012-04-18
  • 打赏
  • 举报
回复
自己顶一个,等解答

23,121

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧